{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cbe47eb3-b4d7-4edc-b0d6-3916ae92a8fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl\n",
    "from perspective.widget import PerspectiveWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "50ca3d34-1f10-4cc7-8f3e-156994c87a55",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (363, 18)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:33:37&quot;</td><td>&quot;000900&quot;</td><td>&quot;现代投资&quot;</td><td>&quot;买入&quot;</td><td>4.05</td><td>34400.0</td><td>139320.0</td><td>22.29</td><td>0.0</td><td>1.39</td><td>0.0</td><td>-139342.29</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>2</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:34:24&quot;</td><td>&quot;601077&quot;</td><td>&quot;渝农商行&quot;</td><td>&quot;买入&quot;</td><td>3.65</td><td>38300.0</td><td>139795.0</td><td>22.37</td><td>0.0</td><td>1.38</td><td>0.0</td><td>-139818.75</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>3</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:36:30&quot;</td><td>&quot;600894&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>4</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:37:25&quot;</td><td>&quot;601992&quot;</td><td>&quot;金隅集团&quot;</td><td>&quot;买入&quot;</td><td>2.59</td><td>54000.0</td><td>139860.0</td><td>22.38</td><td>0.0</td><td>1.42</td><td>0.0</td><td>-139883.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>5</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:38:16&quot;</td><td>&quot;002462&quot;</td><td>&quot;嘉事堂&quot;</td><td>&quot;买入&quot;</td><td>13.51</td><td>10400.0</td><td>140504.0</td><td>22.48</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-140526.48</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>359</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:31:53&quot;</td><td>&quot;002956&quot;</td><td>&quot;西麦食品&quot;</td><td>&quot;卖出&quot;</td><td>14.13</td><td>5000.0</td><td>70650.0</td><td>6.74</td><td>35.35</td><td>0.0</td><td>0.0</td><td>70607.91</td><td>0.000095</td><td>0.0005</td><td>0.0</td></tr><tr><td>360</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:39:57&quot;</td><td>&quot;603214&quot;</td><td>&quot;爱婴室&quot;</td><td>&quot;买入&quot;</td><td>15.84</td><td>3100.0</td><td>49104.0</td><td>5.0</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-49109.51</td><td>0.000102</td><td>0.0</td><td>0.00001</td></tr><tr><td>361</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:40:55&quot;</td><td>&quot;300132&quot;</td><td>&quot;青松股份&quot;</td><td>&quot;买入&quot;</td><td>5.21</td><td>9600.0</td><td>50016.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-50021.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr><tr><td>362</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:43:13&quot;</td><td>&quot;002492&quot;</td><td>&quot;恒基达鑫&quot;</td><td>&quot;买入&quot;</td><td>5.91</td><td>8400.0</td><td>49644.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49649.0</td><td>0.000101</td><td>0.0</td><td>0.0</td></tr><tr><td>363</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:44:45&quot;</td><td>&quot;002111&quot;</td><td>&quot;威海广泰&quot;</td><td>&quot;买入&quot;</td><td>9.24</td><td>5400.0</td><td>49896.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49901.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (363, 18)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ 发生金  ┆ 手续费  ┆ 印花税 ┆ 过户费 │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ 额      ┆ 率      ┆ 率     ┆ 率     │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 1    ┆ 湘财    ┆ 2022-07 ┆ 09:33:3 ┆ … ┆ -139342 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 7       ┆   ┆ .29     ┆         ┆        ┆ 1      │\n",
       "│ 2    ┆ 湘财    ┆ 2022-07 ┆ 09:34:2 ┆ … ┆ -139818 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 4       ┆   ┆ .75     ┆         ┆        ┆ 1      │\n",
       "│ 3    ┆ 湘财    ┆ 2022-07 ┆ 09:36:3 ┆ … ┆ -139979 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 0       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 4    ┆ 湘财    ┆ 2022-07 ┆ 09:37:2 ┆ … ┆ -139883 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 5       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 5    ┆ 湘财    ┆ 2022-07 ┆ 09:38:1 ┆ … ┆ -140526 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 6       ┆   ┆ .48     ┆         ┆        ┆ 1      │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 359  ┆ 海通两  ┆ 2023-10 ┆ 09:31:5 ┆ … ┆ 70607.9 ┆ 0.00009 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 1       ┆ 5       ┆        ┆        │\n",
       "│ 360  ┆ 海通两  ┆ 2023-10 ┆ 09:39:5 ┆ … ┆ -49109. ┆ 0.00010 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -31     ┆ 7       ┆   ┆ 51      ┆ 2       ┆        ┆ 1      │\n",
       "│ 361  ┆ 海通两  ┆ 2023-10 ┆ 09:40:5 ┆ … ┆ -50021. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "│ 362  ┆ 海通两  ┆ 2023-10 ┆ 09:43:1 ┆ … ┆ -49649. ┆ 0.00010 ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 0       ┆ 1       ┆        ┆        │\n",
       "│ 363  ┆ 海通两  ┆ 2023-10 ┆ 09:44:4 ┆ … ┆ -49901. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_parquet(\"stock_trades.parquet\")\n",
    "df = df.sort(\"交易日期\", \"交易时间\", \"证券代码\")\n",
    "df = df.with_columns(\n",
    "    pl.col(\"交易时间\").cast(pl.String),\n",
    "    手续费率=pl.col(\"手续费\") / pl.col(\"成交金额\"),\n",
    "    印花税率=pl.col(\"印花税\") / pl.col(\"成交金额\"),\n",
    "    过户费率=pl.col(\"过户费\") / pl.col(\"成交金额\"),\n",
    ")\n",
    "\n",
    "df = df.with_row_index(\"序号\", 1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "5e91d691-2d29-4ea1-8fc2-90566b67ed6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (2, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>券商</th><th>count</th></tr><tr><td>str</td><td>u32</td></tr></thead><tbody><tr><td>&quot;海通普通&quot;</td><td>33</td></tr><tr><td>&quot;湘财&quot;</td><td>19</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (2, 2)\n",
       "┌──────────┬───────┐\n",
       "│ 券商     ┆ count │\n",
       "│ ---      ┆ ---   │\n",
       "│ str      ┆ u32   │\n",
       "╞══════════╪═══════╡\n",
       "│ 海通普通 ┆ 33    │\n",
       "│ 湘财     ┆ 19    │\n",
       "└──────────┴───────┘"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter(pl.col(\"手续费\") < 5)[\"券商\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bb04bba0-5d44-4e22-b4fc-3664e91a1c81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (53, 18)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>320</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:34:39&quot;</td><td>&quot;300642&quot;</td><td>&quot;透景生命&quot;</td><td>&quot;卖出&quot;</td><td>18.106</td><td>2500.0</td><td>45266.0</td><td>5.0</td><td>45.26</td><td>0.0</td><td>0.0</td><td>45215.74</td><td>0.00011</td><td>0.001</td><td>0.0</td></tr><tr><td>323</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:39:25&quot;</td><td>&quot;600333&quot;</td><td>&quot;长春燃气&quot;</td><td>&quot;卖出&quot;</td><td>5.04</td><td>3900.0</td><td>19656.0</td><td>5.0</td><td>19.64</td><td>0.23</td><td>0.0</td><td>19631.13</td><td>0.000254</td><td>0.000999</td><td>0.000012</td></tr><tr><td>325</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;10:03:59&quot;</td><td>&quot;600525&quot;</td><td>&quot;长园集团&quot;</td><td>&quot;买入&quot;</td><td>4.88</td><td>8500.0</td><td>41480.0</td><td>5.0</td><td>0.0</td><td>0.35</td><td>0.0</td><td>-41485.35</td><td>0.000121</td><td>0.0</td><td>0.000008</td></tr><tr><td>327</td><td>&quot;海通两融&quot;</td><td>2023-09-01</td><td>&quot;10:22:36&quot;</td><td>&quot;300889&quot;</td><td>&quot;爱克股份&quot;</td><td>&quot;卖出&quot;</td><td>15.22</td><td>1500.0</td><td>22830.0</td><td>5.0</td><td>11.42</td><td>0.0</td><td>0.0</td><td>22813.58</td><td>0.000219</td><td>0.0005</td><td>0.0</td></tr><tr><td>329</td><td>&quot;海通两融&quot;</td><td>2023-09-01</td><td>&quot;10:23:28&quot;</td><td>&quot;300889&quot;</td><td>&quot;爱克股份&quot;</td><td>&quot;卖出&quot;</td><td>15.21</td><td>1700.0</td><td>25857.0</td><td>5.0</td><td>12.92</td><td>0.0</td><td>0.0</td><td>25839.08</td><td>0.000193</td><td>0.0005</td><td>0.0</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>322</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:36:13&quot;</td><td>&quot;603967&quot;</td><td>&quot;中创物流&quot;</td><td>&quot;卖出&quot;</td><td>9.36</td><td>10600.0</td><td>99216.0</td><td>10.91</td><td>99.24</td><td>1.0</td><td>0.0</td><td>99104.85</td><td>0.00011</td><td>0.001</td><td>0.00001</td></tr><tr><td>319</td><td>&quot;海通两融&quot;</td><td>2023-08-09</td><td>&quot;10:12:16&quot;</td><td>&quot;600336&quot;</td><td>&quot;澳柯玛&quot;</td><td>&quot;买入&quot;</td><td>5.72</td><td>17400.0</td><td>99528.0</td><td>10.95</td><td>0.0</td><td>1.0</td><td>0.0</td><td>-99539.95</td><td>0.00011</td><td>0.0</td><td>0.00001</td></tr><tr><td>324</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:58:46&quot;</td><td>&quot;603626&quot;</td><td>&quot;科森科技&quot;</td><td>&quot;买入&quot;</td><td>6.35</td><td>15700.0</td><td>99695.0</td><td>10.97</td><td>0.0</td><td>1.02</td><td>0.0</td><td>-99706.99</td><td>0.00011</td><td>0.0</td><td>0.00001</td></tr><tr><td>316</td><td>&quot;海通两融&quot;</td><td>2023-08-04</td><td>&quot;10:07:37&quot;</td><td>&quot;603967&quot;</td><td>&quot;中创物流&quot;</td><td>&quot;买入&quot;</td><td>9.43</td><td>10600.0</td><td>99958.0</td><td>11.0</td><td>0.0</td><td>1.01</td><td>0.0</td><td>-99970.01</td><td>0.00011</td><td>0.0</td><td>0.00001</td></tr><tr><td>340</td><td>&quot;海通两融&quot;</td><td>2023-09-12</td><td>&quot;14:31:39&quot;</td><td>&quot;603626&quot;</td><td>&quot;科森科技&quot;</td><td>&quot;卖出&quot;</td><td>7.43</td><td>15700.0</td><td>116651.0</td><td>11.13</td><td>58.32</td><td>1.17</td><td>0.0</td><td>116580.38</td><td>0.000095</td><td>0.0005</td><td>0.00001</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (53, 18)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ 发生金  ┆ 手续费  ┆ 印花税 ┆ 过户费 │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ 额      ┆ 率      ┆ 率     ┆ 率     │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 320  ┆ 海通两  ┆ 2023-08 ┆ 09:34:3 ┆ … ┆ 45215.7 ┆ 0.00011 ┆ 0.001  ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -22     ┆ 9       ┆   ┆ 4       ┆         ┆        ┆        │\n",
       "│ 323  ┆ 海通两  ┆ 2023-08 ┆ 09:39:2 ┆ … ┆ 19631.1 ┆ 0.00025 ┆ 0.0009 ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 5       ┆   ┆ 3       ┆ 4       ┆ 99     ┆ 12     │\n",
       "│ 325  ┆ 海通两  ┆ 2023-08 ┆ 10:03:5 ┆ … ┆ -41485. ┆ 0.00012 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 9       ┆   ┆ 35      ┆ 1       ┆        ┆ 08     │\n",
       "│ 327  ┆ 海通两  ┆ 2023-09 ┆ 10:22:3 ┆ … ┆ 22813.5 ┆ 0.00021 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -01     ┆ 6       ┆   ┆ 8       ┆ 9       ┆        ┆        │\n",
       "│ 329  ┆ 海通两  ┆ 2023-09 ┆ 10:23:2 ┆ … ┆ 25839.0 ┆ 0.00019 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -01     ┆ 8       ┆   ┆ 8       ┆ 3       ┆        ┆        │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 322  ┆ 海通两  ┆ 2023-08 ┆ 09:36:1 ┆ … ┆ 99104.8 ┆ 0.00011 ┆ 0.001  ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 3       ┆   ┆ 5       ┆         ┆        ┆ 1      │\n",
       "│ 319  ┆ 海通两  ┆ 2023-08 ┆ 10:12:1 ┆ … ┆ -99539. ┆ 0.00011 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -09     ┆ 6       ┆   ┆ 95      ┆         ┆        ┆ 1      │\n",
       "│ 324  ┆ 海通两  ┆ 2023-08 ┆ 09:58:4 ┆ … ┆ -99706. ┆ 0.00011 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 6       ┆   ┆ 99      ┆         ┆        ┆ 1      │\n",
       "│ 316  ┆ 海通两  ┆ 2023-08 ┆ 10:07:3 ┆ … ┆ -99970. ┆ 0.00011 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -04     ┆ 7       ┆   ┆ 01      ┆         ┆        ┆ 1      │\n",
       "│ 340  ┆ 海通两  ┆ 2023-09 ┆ 14:31:3 ┆ … ┆ 116580. ┆ 0.00009 ┆ 0.0005 ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -12     ┆ 9       ┆   ┆ 38      ┆ 5       ┆        ┆ 1      │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter(pl.col(\"券商\") == \"海通两融\").sort(\"手续费\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c3e88d8c-c161-4ed3-be72-14b5fa5332e3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9e493d760a224e8b8d53d1c1d8273a54",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "PerspectiveWidget(binding_mode='server', columns=['序号', '券商', '交易日期', '交易时间', '证券代码', '证券名称', '买卖标志', '成交价格', …"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PerspectiveWidget(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ef0a8ba7-5a19-4c02-82b1-193d60a4c50b",
   "metadata": {},
   "outputs": [],
   "source": [
    "d1 = df.join(\n",
    "    df.group_by(\"证券代码\", \"证券名称\")\n",
    "    .agg(\n",
    "        结余数量=(\n",
    "            pl.when(pl.col(\"买卖标志\") == \"卖出\")\n",
    "            .then(-pl.col(\"成交数量\"))\n",
    "            .when(pl.col(\"买卖标志\") == \"买入\")\n",
    "            .then(pl.col(\"成交数量\"))\n",
    "            .sum()\n",
    "        ),\n",
    "    )\n",
    "    .filter(pl.col(\"结余数量\") < 0),\n",
    "    on=\"证券代码\",\n",
    "    how=\"anti\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f4da212e-f2ca-4f22-a1db-b692c671b5c8",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (358, 18)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:33:37&quot;</td><td>&quot;000900&quot;</td><td>&quot;现代投资&quot;</td><td>&quot;买入&quot;</td><td>4.05</td><td>34400.0</td><td>139320.0</td><td>22.29</td><td>0.0</td><td>1.39</td><td>0.0</td><td>-139342.29</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>2</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:34:24&quot;</td><td>&quot;601077&quot;</td><td>&quot;渝农商行&quot;</td><td>&quot;买入&quot;</td><td>3.65</td><td>38300.0</td><td>139795.0</td><td>22.37</td><td>0.0</td><td>1.38</td><td>0.0</td><td>-139818.75</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>3</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:36:30&quot;</td><td>&quot;600894&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>4</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:37:25&quot;</td><td>&quot;601992&quot;</td><td>&quot;金隅集团&quot;</td><td>&quot;买入&quot;</td><td>2.59</td><td>54000.0</td><td>139860.0</td><td>22.38</td><td>0.0</td><td>1.42</td><td>0.0</td><td>-139883.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>5</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:38:16&quot;</td><td>&quot;002462&quot;</td><td>&quot;嘉事堂&quot;</td><td>&quot;买入&quot;</td><td>13.51</td><td>10400.0</td><td>140504.0</td><td>22.48</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-140526.48</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>359</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:31:53&quot;</td><td>&quot;002956&quot;</td><td>&quot;西麦食品&quot;</td><td>&quot;卖出&quot;</td><td>14.13</td><td>5000.0</td><td>70650.0</td><td>6.74</td><td>35.35</td><td>0.0</td><td>0.0</td><td>70607.91</td><td>0.000095</td><td>0.0005</td><td>0.0</td></tr><tr><td>360</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:39:57&quot;</td><td>&quot;603214&quot;</td><td>&quot;爱婴室&quot;</td><td>&quot;买入&quot;</td><td>15.84</td><td>3100.0</td><td>49104.0</td><td>5.0</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-49109.51</td><td>0.000102</td><td>0.0</td><td>0.00001</td></tr><tr><td>361</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:40:55&quot;</td><td>&quot;300132&quot;</td><td>&quot;青松股份&quot;</td><td>&quot;买入&quot;</td><td>5.21</td><td>9600.0</td><td>50016.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-50021.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr><tr><td>362</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:43:13&quot;</td><td>&quot;002492&quot;</td><td>&quot;恒基达鑫&quot;</td><td>&quot;买入&quot;</td><td>5.91</td><td>8400.0</td><td>49644.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49649.0</td><td>0.000101</td><td>0.0</td><td>0.0</td></tr><tr><td>363</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:44:45&quot;</td><td>&quot;002111&quot;</td><td>&quot;威海广泰&quot;</td><td>&quot;买入&quot;</td><td>9.24</td><td>5400.0</td><td>49896.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49901.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (358, 18)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ 发生金  ┆ 手续费  ┆ 印花税 ┆ 过户费 │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ 额      ┆ 率      ┆ 率     ┆ 率     │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 1    ┆ 湘财    ┆ 2022-07 ┆ 09:33:3 ┆ … ┆ -139342 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 7       ┆   ┆ .29     ┆         ┆        ┆ 1      │\n",
       "│ 2    ┆ 湘财    ┆ 2022-07 ┆ 09:34:2 ┆ … ┆ -139818 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 4       ┆   ┆ .75     ┆         ┆        ┆ 1      │\n",
       "│ 3    ┆ 湘财    ┆ 2022-07 ┆ 09:36:3 ┆ … ┆ -139979 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 0       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 4    ┆ 湘财    ┆ 2022-07 ┆ 09:37:2 ┆ … ┆ -139883 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 5       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 5    ┆ 湘财    ┆ 2022-07 ┆ 09:38:1 ┆ … ┆ -140526 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 6       ┆   ┆ .48     ┆         ┆        ┆ 1      │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 359  ┆ 海通两  ┆ 2023-10 ┆ 09:31:5 ┆ … ┆ 70607.9 ┆ 0.00009 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 1       ┆ 5       ┆        ┆        │\n",
       "│ 360  ┆ 海通两  ┆ 2023-10 ┆ 09:39:5 ┆ … ┆ -49109. ┆ 0.00010 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -31     ┆ 7       ┆   ┆ 51      ┆ 2       ┆        ┆ 1      │\n",
       "│ 361  ┆ 海通两  ┆ 2023-10 ┆ 09:40:5 ┆ … ┆ -50021. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "│ 362  ┆ 海通两  ┆ 2023-10 ┆ 09:43:1 ┆ … ┆ -49649. ┆ 0.00010 ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 0       ┆ 1       ┆        ┆        │\n",
       "│ 363  ┆ 海通两  ┆ 2023-10 ┆ 09:44:4 ┆ … ┆ -49901. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "c2dbc7c8-e99f-41dc-b10d-0f152265fb2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2022, 7, 11)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_date = df[\"交易日期\"].min()\n",
    "start_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "54949829-827b-4441-bd59-083b03867fea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2023, 10, 31)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "end_date = df[\"交易日期\"].max()\n",
    "end_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "7e55bd6e-730d-4f18-9d9b-7045ddce076c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (478, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>交易日期</th></tr><tr><td>date</td></tr></thead><tbody><tr><td>2022-07-11</td></tr><tr><td>2022-07-12</td></tr><tr><td>2022-07-13</td></tr><tr><td>2022-07-14</td></tr><tr><td>2022-07-15</td></tr><tr><td>&hellip;</td></tr><tr><td>2023-10-27</td></tr><tr><td>2023-10-28</td></tr><tr><td>2023-10-29</td></tr><tr><td>2023-10-30</td></tr><tr><td>2023-10-31</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (478, 1)\n",
       "┌────────────┐\n",
       "│ 交易日期   │\n",
       "│ ---        │\n",
       "│ date       │\n",
       "╞════════════╡\n",
       "│ 2022-07-11 │\n",
       "│ 2022-07-12 │\n",
       "│ 2022-07-13 │\n",
       "│ 2022-07-14 │\n",
       "│ 2022-07-15 │\n",
       "│ …          │\n",
       "│ 2023-10-27 │\n",
       "│ 2023-10-28 │\n",
       "│ 2023-10-29 │\n",
       "│ 2023-10-30 │\n",
       "│ 2023-10-31 │\n",
       "└────────────┘"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k1 = pl.select(交易日期=pl.date_range(start_date, end_date))\n",
    "k1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "4c164354-30fd-4888-8193-bb5e2b04a2dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (152, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>证券代码</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;000096&quot;</td></tr><tr><td>&quot;000532&quot;</td></tr><tr><td>&quot;000559&quot;</td></tr><tr><td>&quot;000599&quot;</td></tr><tr><td>&quot;000655&quot;</td></tr><tr><td>&hellip;</td></tr><tr><td>&quot;688299&quot;</td></tr><tr><td>&quot;688321&quot;</td></tr><tr><td>&quot;688360&quot;</td></tr><tr><td>&quot;688393&quot;</td></tr><tr><td>&quot;688660&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (152, 1)\n",
       "┌──────────┐\n",
       "│ 证券代码 │\n",
       "│ ---      │\n",
       "│ str      │\n",
       "╞══════════╡\n",
       "│ 000096   │\n",
       "│ 000532   │\n",
       "│ 000559   │\n",
       "│ 000599   │\n",
       "│ 000655   │\n",
       "│ …        │\n",
       "│ 688299   │\n",
       "│ 688321   │\n",
       "│ 688360   │\n",
       "│ 688393   │\n",
       "│ 688660   │\n",
       "└──────────┘"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k2 = df[\"证券代码\"].unique().sort().to_frame()\n",
    "k2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "565780a0-639f-48f3-b090-4be9ae3a7f4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (2, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>date</th></tr><tr><td>i64</td></tr></thead><tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (2, 1)\n",
       "┌──────┐\n",
       "│ date │\n",
       "│ ---  │\n",
       "│ i64  │\n",
       "╞══════╡\n",
       "│ 1    │\n",
       "│ 2    │\n",
       "└──────┘"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k1 = pl.DataFrame({\"date\": [1, 2]})\n",
    "k1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "fbffd2e7-7e21-4716-9725-f2a9dd783e7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (3, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>name</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;A&quot;</td></tr><tr><td>&quot;B&quot;</td></tr><tr><td>&quot;C&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (3, 1)\n",
       "┌──────┐\n",
       "│ name │\n",
       "│ ---  │\n",
       "│ str  │\n",
       "╞══════╡\n",
       "│ A    │\n",
       "│ B    │\n",
       "│ C    │\n",
       "└──────┘"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k2 = pl.DataFrame({\"name\": [\"A\", \"B\", \"C\"]})\n",
    "k2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8a1cd0ad-ddc3-48d1-8185-1b975f8644a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "k = k1.join(k2, how=\"cross\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "f110fd1b-66ce-4cc1-a7ef-15f45143fbad",
   "metadata": {},
   "outputs": [],
   "source": [
    "d2 = (\n",
    "    k.join(\n",
    "        d1,\n",
    "        left_on=[\"交易日期\", \"证券代码\"],\n",
    "        right_on=[\"交易日期\", \"证券代码\"],\n",
    "        how=\"left\",\n",
    "    )\n",
    "    .sort(\"交易日期\", \"证券代码\")\n",
    "    .with_columns(\n",
    "        结余数量=(\n",
    "            pl.when(pl.col(\"买卖标志\") == \"卖出\")\n",
    "            .then(-pl.col(\"成交数量\"))\n",
    "            .when(pl.col(\"买卖标志\") == \"买入\")\n",
    "            .then(pl.col(\"成交数量\"))\n",
    "            .otherwise(0)\n",
    "            .cum_sum()\n",
    "            .over(\"证券代码\")\n",
    "        ),\n",
    "    )\n",
    "    .filter(pl.col.结余数量 > 0)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "6f06155f-8474-4ac7-bb82-92afe4d6050f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (7, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>n</th><th>running_n</th></tr><tr><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>500</td><td>500</td></tr><tr><td>0</td><td>500</td></tr><tr><td>0</td><td>500</td></tr><tr><td>-200</td><td>300</td></tr><tr><td>300</td><td>600</td></tr><tr><td>0</td><td>600</td></tr><tr><td>-400</td><td>200</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (7, 2)\n",
       "┌──────┬───────────┐\n",
       "│ n    ┆ running_n │\n",
       "│ ---  ┆ ---       │\n",
       "│ i64  ┆ i64       │\n",
       "╞══════╪═══════════╡\n",
       "│ 500  ┆ 500       │\n",
       "│ 0    ┆ 500       │\n",
       "│ 0    ┆ 500       │\n",
       "│ -200 ┆ 300       │\n",
       "│ 300  ┆ 600       │\n",
       "│ 0    ┆ 600       │\n",
       "│ -400 ┆ 200       │\n",
       "└──────┴───────────┘"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = pl.DataFrame({\"n\": [500, 0, 0, -200, 300, 0, -400]})\n",
    "d.with_columns(running_n=pl.col.n.cum_sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "90db4878-2441-498c-8c15-684dde5c91e2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (10, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>n</th><th>name</th><th>runing_n</th></tr><tr><td>i64</td><td>str</td><td>i64</td></tr></thead><tbody><tr><td>500</td><td>&quot;A&quot;</td><td>500</td></tr><tr><td>0</td><td>&quot;A&quot;</td><td>500</td></tr><tr><td>-200</td><td>&quot;A&quot;</td><td>300</td></tr><tr><td>300</td><td>&quot;A&quot;</td><td>600</td></tr><tr><td>-400</td><td>&quot;A&quot;</td><td>200</td></tr><tr><td>800</td><td>&quot;B&quot;</td><td>800</td></tr><tr><td>300</td><td>&quot;B&quot;</td><td>1100</td></tr><tr><td>-200</td><td>&quot;B&quot;</td><td>900</td></tr><tr><td>100</td><td>&quot;B&quot;</td><td>1000</td></tr><tr><td>-400</td><td>&quot;B&quot;</td><td>600</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (10, 3)\n",
       "┌──────┬──────┬──────────┐\n",
       "│ n    ┆ name ┆ runing_n │\n",
       "│ ---  ┆ ---  ┆ ---      │\n",
       "│ i64  ┆ str  ┆ i64      │\n",
       "╞══════╪══════╪══════════╡\n",
       "│ 500  ┆ A    ┆ 500      │\n",
       "│ 0    ┆ A    ┆ 500      │\n",
       "│ -200 ┆ A    ┆ 300      │\n",
       "│ 300  ┆ A    ┆ 600      │\n",
       "│ -400 ┆ A    ┆ 200      │\n",
       "│ 800  ┆ B    ┆ 800      │\n",
       "│ 300  ┆ B    ┆ 1100     │\n",
       "│ -200 ┆ B    ┆ 900      │\n",
       "│ 100  ┆ B    ┆ 1000     │\n",
       "│ -400 ┆ B    ┆ 600      │\n",
       "└──────┴──────┴──────────┘"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd1 = pl.DataFrame({\"n\": [500, 0, -200, 300, -400]}).with_columns(name=pl.lit(\"A\"))\n",
    "dd2 = pl.DataFrame({\"n\": [800, 300, -200, 100, -400]}).with_columns(name=pl.lit(\"B\"))\n",
    "dd = pl.concat([dd1, dd2])\n",
    "dd.with_columns(\n",
    "    runing_n=pl.col.n.cum_sum().over(\"name\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "c154da11-2aa6-443f-8b3c-0753ffa374d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4a66af832e614c5492b158174445b63a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "PerspectiveWidget(binding_mode='server', columns=['交易日期', '证券代码', '序号', '券商', '交易时间', '证券名称', '买卖标志', '成交价格', …"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PerspectiveWidget(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d3c2bf07-0ca3-428a-9288-315112bb5249",
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl\n",
    "from perspective.widget import PerspectiveWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d75103f9-718b-46ed-9ecb-ec855e96f821",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (363, 18)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:33:37&quot;</td><td>&quot;000900&quot;</td><td>&quot;现代投资&quot;</td><td>&quot;买入&quot;</td><td>4.05</td><td>34400.0</td><td>139320.0</td><td>22.29</td><td>0.0</td><td>1.39</td><td>0.0</td><td>-139342.29</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>2</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:34:24&quot;</td><td>&quot;601077&quot;</td><td>&quot;渝农商行&quot;</td><td>&quot;买入&quot;</td><td>3.65</td><td>38300.0</td><td>139795.0</td><td>22.37</td><td>0.0</td><td>1.38</td><td>0.0</td><td>-139818.75</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>3</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:36:30&quot;</td><td>&quot;600894&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>4</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:37:25&quot;</td><td>&quot;601992&quot;</td><td>&quot;金隅集团&quot;</td><td>&quot;买入&quot;</td><td>2.59</td><td>54000.0</td><td>139860.0</td><td>22.38</td><td>0.0</td><td>1.42</td><td>0.0</td><td>-139883.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>5</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:38:16&quot;</td><td>&quot;002462&quot;</td><td>&quot;嘉事堂&quot;</td><td>&quot;买入&quot;</td><td>13.51</td><td>10400.0</td><td>140504.0</td><td>22.48</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-140526.48</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>359</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:31:53&quot;</td><td>&quot;002956&quot;</td><td>&quot;西麦食品&quot;</td><td>&quot;卖出&quot;</td><td>14.13</td><td>5000.0</td><td>70650.0</td><td>6.74</td><td>35.35</td><td>0.0</td><td>0.0</td><td>70607.91</td><td>0.000095</td><td>0.0005</td><td>0.0</td></tr><tr><td>360</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:39:57&quot;</td><td>&quot;603214&quot;</td><td>&quot;爱婴室&quot;</td><td>&quot;买入&quot;</td><td>15.84</td><td>3100.0</td><td>49104.0</td><td>5.0</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-49109.51</td><td>0.000102</td><td>0.0</td><td>0.00001</td></tr><tr><td>361</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:40:55&quot;</td><td>&quot;300132&quot;</td><td>&quot;青松股份&quot;</td><td>&quot;买入&quot;</td><td>5.21</td><td>9600.0</td><td>50016.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-50021.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr><tr><td>362</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:43:13&quot;</td><td>&quot;002492&quot;</td><td>&quot;恒基达鑫&quot;</td><td>&quot;买入&quot;</td><td>5.91</td><td>8400.0</td><td>49644.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49649.0</td><td>0.000101</td><td>0.0</td><td>0.0</td></tr><tr><td>363</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:44:45&quot;</td><td>&quot;002111&quot;</td><td>&quot;威海广泰&quot;</td><td>&quot;买入&quot;</td><td>9.24</td><td>5400.0</td><td>49896.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49901.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (363, 18)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ 发生金  ┆ 手续费  ┆ 印花税 ┆ 过户费 │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ 额      ┆ 率      ┆ 率     ┆ 率     │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 1    ┆ 湘财    ┆ 2022-07 ┆ 09:33:3 ┆ … ┆ -139342 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 7       ┆   ┆ .29     ┆         ┆        ┆ 1      │\n",
       "│ 2    ┆ 湘财    ┆ 2022-07 ┆ 09:34:2 ┆ … ┆ -139818 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 4       ┆   ┆ .75     ┆         ┆        ┆ 1      │\n",
       "│ 3    ┆ 湘财    ┆ 2022-07 ┆ 09:36:3 ┆ … ┆ -139979 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 0       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 4    ┆ 湘财    ┆ 2022-07 ┆ 09:37:2 ┆ … ┆ -139883 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 5       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 5    ┆ 湘财    ┆ 2022-07 ┆ 09:38:1 ┆ … ┆ -140526 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 6       ┆   ┆ .48     ┆         ┆        ┆ 1      │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 359  ┆ 海通两  ┆ 2023-10 ┆ 09:31:5 ┆ … ┆ 70607.9 ┆ 0.00009 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 1       ┆ 5       ┆        ┆        │\n",
       "│ 360  ┆ 海通两  ┆ 2023-10 ┆ 09:39:5 ┆ … ┆ -49109. ┆ 0.00010 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -31     ┆ 7       ┆   ┆ 51      ┆ 2       ┆        ┆ 1      │\n",
       "│ 361  ┆ 海通两  ┆ 2023-10 ┆ 09:40:5 ┆ … ┆ -50021. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "│ 362  ┆ 海通两  ┆ 2023-10 ┆ 09:43:1 ┆ … ┆ -49649. ┆ 0.00010 ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 0       ┆ 1       ┆        ┆        │\n",
       "│ 363  ┆ 海通两  ┆ 2023-10 ┆ 09:44:4 ┆ … ┆ -49901. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_parquet(\"stock_trades.parquet\")\n",
    "df = df.sort(\"交易日期\", \"交易时间\", \"证券代码\")\n",
    "df = df.with_columns(\n",
    "    pl.col(\"交易时间\").cast(pl.String),\n",
    "    手续费率=pl.col(\"手续费\") / pl.col(\"成交金额\"),\n",
    "    印花税率=pl.col(\"印花税\") / pl.col(\"成交金额\"),\n",
    "    过户费率=pl.col(\"过户费\") / pl.col(\"成交金额\"),\n",
    ")\n",
    "\n",
    "df = df.with_row_index(\"序号\", 1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "27bab867-6011-4fd6-8182-b71d6043f63c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (2, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>券商</th><th>count</th></tr><tr><td>str</td><td>u32</td></tr></thead><tbody><tr><td>&quot;湘财&quot;</td><td>19</td></tr><tr><td>&quot;海通普通&quot;</td><td>33</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (2, 2)\n",
       "┌──────────┬───────┐\n",
       "│ 券商     ┆ count │\n",
       "│ ---      ┆ ---   │\n",
       "│ str      ┆ u32   │\n",
       "╞══════════╪═══════╡\n",
       "│ 湘财     ┆ 19    │\n",
       "│ 海通普通 ┆ 33    │\n",
       "└──────────┴───────┘"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter(pl.col(\"手续费\") < 5)[\"券商\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f9ff5691-88b5-44cb-98cb-420c3d8b8d72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (53, 18)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>320</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:34:39&quot;</td><td>&quot;300642&quot;</td><td>&quot;透景生命&quot;</td><td>&quot;卖出&quot;</td><td>18.106</td><td>2500.0</td><td>45266.0</td><td>5.0</td><td>45.26</td><td>0.0</td><td>0.0</td><td>45215.74</td><td>0.00011</td><td>0.001</td><td>0.0</td></tr><tr><td>323</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:39:25&quot;</td><td>&quot;600333&quot;</td><td>&quot;长春燃气&quot;</td><td>&quot;卖出&quot;</td><td>5.04</td><td>3900.0</td><td>19656.0</td><td>5.0</td><td>19.64</td><td>0.23</td><td>0.0</td><td>19631.13</td><td>0.000254</td><td>0.000999</td><td>0.000012</td></tr><tr><td>325</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;10:03:59&quot;</td><td>&quot;600525&quot;</td><td>&quot;长园集团&quot;</td><td>&quot;买入&quot;</td><td>4.88</td><td>8500.0</td><td>41480.0</td><td>5.0</td><td>0.0</td><td>0.35</td><td>0.0</td><td>-41485.35</td><td>0.000121</td><td>0.0</td><td>0.000008</td></tr><tr><td>327</td><td>&quot;海通两融&quot;</td><td>2023-09-01</td><td>&quot;10:22:36&quot;</td><td>&quot;300889&quot;</td><td>&quot;爱克股份&quot;</td><td>&quot;卖出&quot;</td><td>15.22</td><td>1500.0</td><td>22830.0</td><td>5.0</td><td>11.42</td><td>0.0</td><td>0.0</td><td>22813.58</td><td>0.000219</td><td>0.0005</td><td>0.0</td></tr><tr><td>329</td><td>&quot;海通两融&quot;</td><td>2023-09-01</td><td>&quot;10:23:28&quot;</td><td>&quot;300889&quot;</td><td>&quot;爱克股份&quot;</td><td>&quot;卖出&quot;</td><td>15.21</td><td>1700.0</td><td>25857.0</td><td>5.0</td><td>12.92</td><td>0.0</td><td>0.0</td><td>25839.08</td><td>0.000193</td><td>0.0005</td><td>0.0</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>322</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:36:13&quot;</td><td>&quot;603967&quot;</td><td>&quot;中创物流&quot;</td><td>&quot;卖出&quot;</td><td>9.36</td><td>10600.0</td><td>99216.0</td><td>10.91</td><td>99.24</td><td>1.0</td><td>0.0</td><td>99104.85</td><td>0.00011</td><td>0.001</td><td>0.00001</td></tr><tr><td>319</td><td>&quot;海通两融&quot;</td><td>2023-08-09</td><td>&quot;10:12:16&quot;</td><td>&quot;600336&quot;</td><td>&quot;澳柯玛&quot;</td><td>&quot;买入&quot;</td><td>5.72</td><td>17400.0</td><td>99528.0</td><td>10.95</td><td>0.0</td><td>1.0</td><td>0.0</td><td>-99539.95</td><td>0.00011</td><td>0.0</td><td>0.00001</td></tr><tr><td>324</td><td>&quot;海通两融&quot;</td><td>2023-08-22</td><td>&quot;09:58:46&quot;</td><td>&quot;603626&quot;</td><td>&quot;科森科技&quot;</td><td>&quot;买入&quot;</td><td>6.35</td><td>15700.0</td><td>99695.0</td><td>10.97</td><td>0.0</td><td>1.02</td><td>0.0</td><td>-99706.99</td><td>0.00011</td><td>0.0</td><td>0.00001</td></tr><tr><td>316</td><td>&quot;海通两融&quot;</td><td>2023-08-04</td><td>&quot;10:07:37&quot;</td><td>&quot;603967&quot;</td><td>&quot;中创物流&quot;</td><td>&quot;买入&quot;</td><td>9.43</td><td>10600.0</td><td>99958.0</td><td>11.0</td><td>0.0</td><td>1.01</td><td>0.0</td><td>-99970.01</td><td>0.00011</td><td>0.0</td><td>0.00001</td></tr><tr><td>340</td><td>&quot;海通两融&quot;</td><td>2023-09-12</td><td>&quot;14:31:39&quot;</td><td>&quot;603626&quot;</td><td>&quot;科森科技&quot;</td><td>&quot;卖出&quot;</td><td>7.43</td><td>15700.0</td><td>116651.0</td><td>11.13</td><td>58.32</td><td>1.17</td><td>0.0</td><td>116580.38</td><td>0.000095</td><td>0.0005</td><td>0.00001</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (53, 18)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ 发生金  ┆ 手续费  ┆ 印花税 ┆ 过户费 │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ 额      ┆ 率      ┆ 率     ┆ 率     │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 320  ┆ 海通两  ┆ 2023-08 ┆ 09:34:3 ┆ … ┆ 45215.7 ┆ 0.00011 ┆ 0.001  ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -22     ┆ 9       ┆   ┆ 4       ┆         ┆        ┆        │\n",
       "│ 323  ┆ 海通两  ┆ 2023-08 ┆ 09:39:2 ┆ … ┆ 19631.1 ┆ 0.00025 ┆ 0.0009 ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 5       ┆   ┆ 3       ┆ 4       ┆ 99     ┆ 12     │\n",
       "│ 325  ┆ 海通两  ┆ 2023-08 ┆ 10:03:5 ┆ … ┆ -41485. ┆ 0.00012 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 9       ┆   ┆ 35      ┆ 1       ┆        ┆ 08     │\n",
       "│ 327  ┆ 海通两  ┆ 2023-09 ┆ 10:22:3 ┆ … ┆ 22813.5 ┆ 0.00021 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -01     ┆ 6       ┆   ┆ 8       ┆ 9       ┆        ┆        │\n",
       "│ 329  ┆ 海通两  ┆ 2023-09 ┆ 10:23:2 ┆ … ┆ 25839.0 ┆ 0.00019 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -01     ┆ 8       ┆   ┆ 8       ┆ 3       ┆        ┆        │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 322  ┆ 海通两  ┆ 2023-08 ┆ 09:36:1 ┆ … ┆ 99104.8 ┆ 0.00011 ┆ 0.001  ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 3       ┆   ┆ 5       ┆         ┆        ┆ 1      │\n",
       "│ 319  ┆ 海通两  ┆ 2023-08 ┆ 10:12:1 ┆ … ┆ -99539. ┆ 0.00011 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -09     ┆ 6       ┆   ┆ 95      ┆         ┆        ┆ 1      │\n",
       "│ 324  ┆ 海通两  ┆ 2023-08 ┆ 09:58:4 ┆ … ┆ -99706. ┆ 0.00011 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -22     ┆ 6       ┆   ┆ 99      ┆         ┆        ┆ 1      │\n",
       "│ 316  ┆ 海通两  ┆ 2023-08 ┆ 10:07:3 ┆ … ┆ -99970. ┆ 0.00011 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -04     ┆ 7       ┆   ┆ 01      ┆         ┆        ┆ 1      │\n",
       "│ 340  ┆ 海通两  ┆ 2023-09 ┆ 14:31:3 ┆ … ┆ 116580. ┆ 0.00009 ┆ 0.0005 ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -12     ┆ 9       ┆   ┆ 38      ┆ 5       ┆        ┆ 1      │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter(pl.col(\"券商\") == \"海通两融\").sort(\"手续费\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bb086939-deb4-48bb-a928-38de3c48eba7",
   "metadata": {},
   "outputs": [],
   "source": [
    "d1 = df.join(\n",
    "    df.group_by(\"证券代码\", \"证券名称\")\n",
    "    .agg(\n",
    "        结余数量=(\n",
    "            pl.when(pl.col(\"买卖标志\") == \"卖出\")\n",
    "            .then(-pl.col(\"成交数量\"))\n",
    "            .when(pl.col(\"买卖标志\") == \"买入\")\n",
    "            .then(pl.col(\"成交数量\"))\n",
    "            .sum()\n",
    "        ),\n",
    "    )\n",
    "    .filter(pl.col(\"结余数量\") < 0),\n",
    "    on=\"证券代码\",\n",
    "    how=\"anti\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1905db0d-a1fb-42ed-9cba-41d1367dba38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2022, 7, 11)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_date = df[\"交易日期\"].min()\n",
    "start_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a5cdac4b-f9df-4c22-a925-fceee69c3c89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2023, 10, 31)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "end_date = df[\"交易日期\"].max()\n",
    "end_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4f1cce40-5a05-4be1-821f-56f88909d2cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (478, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>交易日期</th></tr><tr><td>date</td></tr></thead><tbody><tr><td>2022-07-11</td></tr><tr><td>2022-07-12</td></tr><tr><td>2022-07-13</td></tr><tr><td>2022-07-14</td></tr><tr><td>2022-07-15</td></tr><tr><td>&hellip;</td></tr><tr><td>2023-10-27</td></tr><tr><td>2023-10-28</td></tr><tr><td>2023-10-29</td></tr><tr><td>2023-10-30</td></tr><tr><td>2023-10-31</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (478, 1)\n",
       "┌────────────┐\n",
       "│ 交易日期   │\n",
       "│ ---        │\n",
       "│ date       │\n",
       "╞════════════╡\n",
       "│ 2022-07-11 │\n",
       "│ 2022-07-12 │\n",
       "│ 2022-07-13 │\n",
       "│ 2022-07-14 │\n",
       "│ 2022-07-15 │\n",
       "│ …          │\n",
       "│ 2023-10-27 │\n",
       "│ 2023-10-28 │\n",
       "│ 2023-10-29 │\n",
       "│ 2023-10-30 │\n",
       "│ 2023-10-31 │\n",
       "└────────────┘"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k1 = pl.select(交易日期=pl.date_range(start_date, end_date))\n",
    "k1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eeeaab6d-6a7a-499a-8396-15847dc781ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (152, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>证券代码</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;000096&quot;</td></tr><tr><td>&quot;000532&quot;</td></tr><tr><td>&quot;000559&quot;</td></tr><tr><td>&quot;000599&quot;</td></tr><tr><td>&quot;000655&quot;</td></tr><tr><td>&hellip;</td></tr><tr><td>&quot;688299&quot;</td></tr><tr><td>&quot;688321&quot;</td></tr><tr><td>&quot;688360&quot;</td></tr><tr><td>&quot;688393&quot;</td></tr><tr><td>&quot;688660&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (152, 1)\n",
       "┌──────────┐\n",
       "│ 证券代码 │\n",
       "│ ---      │\n",
       "│ str      │\n",
       "╞══════════╡\n",
       "│ 000096   │\n",
       "│ 000532   │\n",
       "│ 000559   │\n",
       "│ 000599   │\n",
       "│ 000655   │\n",
       "│ …        │\n",
       "│ 688299   │\n",
       "│ 688321   │\n",
       "│ 688360   │\n",
       "│ 688393   │\n",
       "│ 688660   │\n",
       "└──────────┘"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k2 = df[\"证券代码\"].unique().sort().to_frame()\n",
    "k2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4736980b-a90e-49ce-a04d-b0008d25f8fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "k = k1.join(k2, how=\"cross\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "556e1bdb-61f3-47c4-a0c0-1c1c2ed1ce8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "d2 = (\n",
    "    k.join(\n",
    "        d1,\n",
    "        left_on=[\"交易日期\", \"证券代码\"],\n",
    "        right_on=[\"交易日期\", \"证券代码\"],\n",
    "        how=\"left\",\n",
    "    )\n",
    "    .sort(\"交易日期\", \"证券代码\")\n",
    "    .with_columns(\n",
    "        结余数量=(\n",
    "            pl.when(pl.col(\"买卖标志\") == \"卖出\")\n",
    "            .then(-pl.col(\"成交数量\"))\n",
    "            .when(pl.col(\"买卖标志\") == \"买入\")\n",
    "            .then(pl.col(\"成交数量\"))\n",
    "            .otherwise(0)\n",
    "            .cum_sum()\n",
    "            .over(\"证券代码\")\n",
    "        ),\n",
    "    )\n",
    "    .filter(pl.col.结余数量 > 0)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d817b1a2-ed48-436a-96bb-5c819316cf16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (7, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>n</th><th>running_n</th></tr><tr><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>500</td><td>500</td></tr><tr><td>0</td><td>500</td></tr><tr><td>0</td><td>500</td></tr><tr><td>-200</td><td>300</td></tr><tr><td>300</td><td>600</td></tr><tr><td>0</td><td>600</td></tr><tr><td>-400</td><td>200</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (7, 2)\n",
       "┌──────┬───────────┐\n",
       "│ n    ┆ running_n │\n",
       "│ ---  ┆ ---       │\n",
       "│ i64  ┆ i64       │\n",
       "╞══════╪═══════════╡\n",
       "│ 500  ┆ 500       │\n",
       "│ 0    ┆ 500       │\n",
       "│ 0    ┆ 500       │\n",
       "│ -200 ┆ 300       │\n",
       "│ 300  ┆ 600       │\n",
       "│ 0    ┆ 600       │\n",
       "│ -400 ┆ 200       │\n",
       "└──────┴───────────┘"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = pl.DataFrame({\"n\": [500, 0, 0, -200, 300, 0, -400]})\n",
    "d.with_columns(running_n=pl.col.n.cum_sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d3f2e6c8-23c1-43eb-9c97-d423a735b3e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (10, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>n</th><th>name</th><th>runing_n</th></tr><tr><td>i64</td><td>str</td><td>i64</td></tr></thead><tbody><tr><td>500</td><td>&quot;A&quot;</td><td>500</td></tr><tr><td>0</td><td>&quot;A&quot;</td><td>500</td></tr><tr><td>-200</td><td>&quot;A&quot;</td><td>300</td></tr><tr><td>300</td><td>&quot;A&quot;</td><td>600</td></tr><tr><td>-400</td><td>&quot;A&quot;</td><td>200</td></tr><tr><td>800</td><td>&quot;B&quot;</td><td>800</td></tr><tr><td>300</td><td>&quot;B&quot;</td><td>1100</td></tr><tr><td>-200</td><td>&quot;B&quot;</td><td>900</td></tr><tr><td>100</td><td>&quot;B&quot;</td><td>1000</td></tr><tr><td>-400</td><td>&quot;B&quot;</td><td>600</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (10, 3)\n",
       "┌──────┬──────┬──────────┐\n",
       "│ n    ┆ name ┆ runing_n │\n",
       "│ ---  ┆ ---  ┆ ---      │\n",
       "│ i64  ┆ str  ┆ i64      │\n",
       "╞══════╪══════╪══════════╡\n",
       "│ 500  ┆ A    ┆ 500      │\n",
       "│ 0    ┆ A    ┆ 500      │\n",
       "│ -200 ┆ A    ┆ 300      │\n",
       "│ 300  ┆ A    ┆ 600      │\n",
       "│ -400 ┆ A    ┆ 200      │\n",
       "│ 800  ┆ B    ┆ 800      │\n",
       "│ 300  ┆ B    ┆ 1100     │\n",
       "│ -200 ┆ B    ┆ 900      │\n",
       "│ 100  ┆ B    ┆ 1000     │\n",
       "│ -400 ┆ B    ┆ 600      │\n",
       "└──────┴──────┴──────────┘"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd1 = pl.DataFrame({\"n\": [500, 0, -200, 300, -400]}).with_columns(name=pl.lit(\"A\"))\n",
    "dd2 = pl.DataFrame({\"n\": [800, 300, -200, 100, -400]}).with_columns(name=pl.lit(\"B\"))\n",
    "dd = pl.concat([dd1, dd2])\n",
    "dd.with_columns(\n",
    "    runing_n=pl.col.n.cum_sum().over(\"name\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8f796cd2-55e2-4a47-9a39-ab00081c51b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d1c61680-9fc2-4101-8291-0e4344df2350",
   "metadata": {},
   "outputs": [],
   "source": [
    "pro = ts.pro_api()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6b523fb0-5732-4f5d-9d59-721c6be8fb61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2022, 7, 11)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e14e4f6d-944e-4002-b315-9f94b4e446ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2023, 10, 31)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "end_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2710148a-9f43-4d4e-8a34-00c096156253",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20220711'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"{start_date:%Y%m%d}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "274cee8e-5238-4166-82fc-c554a8e9ee35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20231031'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(end_date, \"%Y%m%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "076ca449-3195-4160-90c1-882fb98c34dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (318, 11)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ts_code</th><th>trade_date</th><th>open</th><th>high</th><th>low</th><th>close</th><th>pre_close</th><th>change</th><th>pct_chg</th><th>vol</th><th>amount</th></tr><tr><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20231031&quot;</td><td>14.75</td><td>14.9</td><td>14.59</td><td>14.7</td><td>14.75</td><td>-0.05</td><td>-0.339</td><td>65859.96</td><td>96984.271</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20231030&quot;</td><td>13.88</td><td>14.89</td><td>13.88</td><td>14.75</td><td>13.93</td><td>0.82</td><td>5.8866</td><td>123932.16</td><td>180119.372</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20231027&quot;</td><td>13.7</td><td>13.98</td><td>13.51</td><td>13.93</td><td>13.64</td><td>0.29</td><td>2.1261</td><td>35782.0</td><td>49386.168</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20231026&quot;</td><td>13.49</td><td>13.68</td><td>13.4</td><td>13.64</td><td>13.62</td><td>0.02</td><td>0.1468</td><td>19215.0</td><td>26005.866</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20231025&quot;</td><td>13.65</td><td>13.77</td><td>13.58</td><td>13.62</td><td>13.67</td><td>-0.05</td><td>-0.3658</td><td>18484.0</td><td>25274.163</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20220715&quot;</td><td>13.61</td><td>13.66</td><td>13.12</td><td>13.13</td><td>13.59</td><td>-0.46</td><td>-3.3848</td><td>32967.65</td><td>44114.064</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20220714&quot;</td><td>13.54</td><td>13.75</td><td>13.5</td><td>13.59</td><td>13.54</td><td>0.05</td><td>0.3693</td><td>21967.0</td><td>29851.164</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20220713&quot;</td><td>13.55</td><td>13.63</td><td>13.39</td><td>13.54</td><td>13.61</td><td>-0.07</td><td>-0.5143</td><td>22793.0</td><td>30714.624</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20220712&quot;</td><td>13.65</td><td>13.69</td><td>13.41</td><td>13.61</td><td>13.65</td><td>-0.04</td><td>-0.293</td><td>29679.0</td><td>40146.31</td></tr><tr><td>&quot;002462.SZ&quot;</td><td>&quot;20220711&quot;</td><td>13.2</td><td>13.96</td><td>13.07</td><td>13.65</td><td>13.18</td><td>0.47</td><td>3.566</td><td>62827.0</td><td>85869.11</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (318, 11)\n",
       "┌─────────┬─────────┬───────┬───────┬───┬────────┬─────────┬─────────┬─────────┐\n",
       "│ ts_code ┆ trade_d ┆ open  ┆ high  ┆ … ┆ change ┆ pct_chg ┆ vol     ┆ amount  │\n",
       "│ ---     ┆ ate     ┆ ---   ┆ ---   ┆   ┆ ---    ┆ ---     ┆ ---     ┆ ---     │\n",
       "│ str     ┆ ---     ┆ f64   ┆ f64   ┆   ┆ f64    ┆ f64     ┆ f64     ┆ f64     │\n",
       "│         ┆ str     ┆       ┆       ┆   ┆        ┆         ┆         ┆         │\n",
       "╞═════════╪═════════╪═══════╪═══════╪═══╪════════╪═════════╪═════════╪═════════╡\n",
       "│ 002462. ┆ 2023103 ┆ 14.75 ┆ 14.9  ┆ … ┆ -0.05  ┆ -0.339  ┆ 65859.9 ┆ 96984.2 │\n",
       "│ SZ      ┆ 1       ┆       ┆       ┆   ┆        ┆         ┆ 6       ┆ 71      │\n",
       "│ 002462. ┆ 2023103 ┆ 13.88 ┆ 14.89 ┆ … ┆ 0.82   ┆ 5.8866  ┆ 123932. ┆ 180119. │\n",
       "│ SZ      ┆ 0       ┆       ┆       ┆   ┆        ┆         ┆ 16      ┆ 372     │\n",
       "│ 002462. ┆ 2023102 ┆ 13.7  ┆ 13.98 ┆ … ┆ 0.29   ┆ 2.1261  ┆ 35782.0 ┆ 49386.1 │\n",
       "│ SZ      ┆ 7       ┆       ┆       ┆   ┆        ┆         ┆         ┆ 68      │\n",
       "│ 002462. ┆ 2023102 ┆ 13.49 ┆ 13.68 ┆ … ┆ 0.02   ┆ 0.1468  ┆ 19215.0 ┆ 26005.8 │\n",
       "│ SZ      ┆ 6       ┆       ┆       ┆   ┆        ┆         ┆         ┆ 66      │\n",
       "│ 002462. ┆ 2023102 ┆ 13.65 ┆ 13.77 ┆ … ┆ -0.05  ┆ -0.3658 ┆ 18484.0 ┆ 25274.1 │\n",
       "│ SZ      ┆ 5       ┆       ┆       ┆   ┆        ┆         ┆         ┆ 63      │\n",
       "│ …       ┆ …       ┆ …     ┆ …     ┆ … ┆ …      ┆ …       ┆ …       ┆ …       │\n",
       "│ 002462. ┆ 2022071 ┆ 13.61 ┆ 13.66 ┆ … ┆ -0.46  ┆ -3.3848 ┆ 32967.6 ┆ 44114.0 │\n",
       "│ SZ      ┆ 5       ┆       ┆       ┆   ┆        ┆         ┆ 5       ┆ 64      │\n",
       "│ 002462. ┆ 2022071 ┆ 13.54 ┆ 13.75 ┆ … ┆ 0.05   ┆ 0.3693  ┆ 21967.0 ┆ 29851.1 │\n",
       "│ SZ      ┆ 4       ┆       ┆       ┆   ┆        ┆         ┆         ┆ 64      │\n",
       "│ 002462. ┆ 2022071 ┆ 13.55 ┆ 13.63 ┆ … ┆ -0.07  ┆ -0.5143 ┆ 22793.0 ┆ 30714.6 │\n",
       "│ SZ      ┆ 3       ┆       ┆       ┆   ┆        ┆         ┆         ┆ 24      │\n",
       "│ 002462. ┆ 2022071 ┆ 13.65 ┆ 13.69 ┆ … ┆ -0.04  ┆ -0.293  ┆ 29679.0 ┆ 40146.3 │\n",
       "│ SZ      ┆ 2       ┆       ┆       ┆   ┆        ┆         ┆         ┆ 1       │\n",
       "│ 002462. ┆ 2022071 ┆ 13.2  ┆ 13.96 ┆ … ┆ 0.47   ┆ 3.566   ┆ 62827.0 ┆ 85869.1 │\n",
       "│ SZ      ┆ 1       ┆       ┆       ┆   ┆        ┆         ┆         ┆ 1       │\n",
       "└─────────┴─────────┴───────┴───────┴───┴────────┴─────────┴─────────┴─────────┘"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hq = pro.daily(\n",
    "    ts_code=\"002462.SZ\",\n",
    "    start_date=format(start_date, \"%Y%m%d\"),\n",
    "    end_date=format(end_date, \"%Y%m%d\"),\n",
    ")\n",
    "hq = pl.from_pandas(hq)\n",
    "hq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6b52cb11-cfce-451b-b20a-b443fc096bf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (358, 18)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:33:37&quot;</td><td>&quot;000900&quot;</td><td>&quot;现代投资&quot;</td><td>&quot;买入&quot;</td><td>4.05</td><td>34400.0</td><td>139320.0</td><td>22.29</td><td>0.0</td><td>1.39</td><td>0.0</td><td>-139342.29</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>2</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:34:24&quot;</td><td>&quot;601077&quot;</td><td>&quot;渝农商行&quot;</td><td>&quot;买入&quot;</td><td>3.65</td><td>38300.0</td><td>139795.0</td><td>22.37</td><td>0.0</td><td>1.38</td><td>0.0</td><td>-139818.75</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>3</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:36:30&quot;</td><td>&quot;600894&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>4</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:37:25&quot;</td><td>&quot;601992&quot;</td><td>&quot;金隅集团&quot;</td><td>&quot;买入&quot;</td><td>2.59</td><td>54000.0</td><td>139860.0</td><td>22.38</td><td>0.0</td><td>1.42</td><td>0.0</td><td>-139883.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>5</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:38:16&quot;</td><td>&quot;002462&quot;</td><td>&quot;嘉事堂&quot;</td><td>&quot;买入&quot;</td><td>13.51</td><td>10400.0</td><td>140504.0</td><td>22.48</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-140526.48</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>359</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:31:53&quot;</td><td>&quot;002956&quot;</td><td>&quot;西麦食品&quot;</td><td>&quot;卖出&quot;</td><td>14.13</td><td>5000.0</td><td>70650.0</td><td>6.74</td><td>35.35</td><td>0.0</td><td>0.0</td><td>70607.91</td><td>0.000095</td><td>0.0005</td><td>0.0</td></tr><tr><td>360</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:39:57&quot;</td><td>&quot;603214&quot;</td><td>&quot;爱婴室&quot;</td><td>&quot;买入&quot;</td><td>15.84</td><td>3100.0</td><td>49104.0</td><td>5.0</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-49109.51</td><td>0.000102</td><td>0.0</td><td>0.00001</td></tr><tr><td>361</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:40:55&quot;</td><td>&quot;300132&quot;</td><td>&quot;青松股份&quot;</td><td>&quot;买入&quot;</td><td>5.21</td><td>9600.0</td><td>50016.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-50021.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr><tr><td>362</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:43:13&quot;</td><td>&quot;002492&quot;</td><td>&quot;恒基达鑫&quot;</td><td>&quot;买入&quot;</td><td>5.91</td><td>8400.0</td><td>49644.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49649.0</td><td>0.000101</td><td>0.0</td><td>0.0</td></tr><tr><td>363</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:44:45&quot;</td><td>&quot;002111&quot;</td><td>&quot;威海广泰&quot;</td><td>&quot;买入&quot;</td><td>9.24</td><td>5400.0</td><td>49896.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49901.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (358, 18)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ 发生金  ┆ 手续费  ┆ 印花税 ┆ 过户费 │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ 额      ┆ 率      ┆ 率     ┆ 率     │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 1    ┆ 湘财    ┆ 2022-07 ┆ 09:33:3 ┆ … ┆ -139342 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 7       ┆   ┆ .29     ┆         ┆        ┆ 1      │\n",
       "│ 2    ┆ 湘财    ┆ 2022-07 ┆ 09:34:2 ┆ … ┆ -139818 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 4       ┆   ┆ .75     ┆         ┆        ┆ 1      │\n",
       "│ 3    ┆ 湘财    ┆ 2022-07 ┆ 09:36:3 ┆ … ┆ -139979 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 0       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 4    ┆ 湘财    ┆ 2022-07 ┆ 09:37:2 ┆ … ┆ -139883 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 5       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 5    ┆ 湘财    ┆ 2022-07 ┆ 09:38:1 ┆ … ┆ -140526 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 6       ┆   ┆ .48     ┆         ┆        ┆ 1      │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 359  ┆ 海通两  ┆ 2023-10 ┆ 09:31:5 ┆ … ┆ 70607.9 ┆ 0.00009 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 1       ┆ 5       ┆        ┆        │\n",
       "│ 360  ┆ 海通两  ┆ 2023-10 ┆ 09:39:5 ┆ … ┆ -49109. ┆ 0.00010 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -31     ┆ 7       ┆   ┆ 51      ┆ 2       ┆        ┆ 1      │\n",
       "│ 361  ┆ 海通两  ┆ 2023-10 ┆ 09:40:5 ┆ … ┆ -50021. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "│ 362  ┆ 海通两  ┆ 2023-10 ┆ 09:43:1 ┆ … ┆ -49649. ┆ 0.00010 ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 0       ┆ 1       ┆        ┆        │\n",
       "│ 363  ┆ 海通两  ┆ 2023-10 ┆ 09:44:4 ┆ … ┆ -49901. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "dd4955d6-66ff-4b62-9ef6-55468813d2db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (4_489, 19)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>交易日期</th><th>证券代码</th><th>序号</th><th>券商</th><th>交易时间</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th><th>结余数量</th></tr><tr><td>date</td><td>str</td><td>u32</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>2022-07-11</td><td>&quot;000900&quot;</td><td>1</td><td>&quot;湘财&quot;</td><td>&quot;09:33:37&quot;</td><td>&quot;现代投资&quot;</td><td>&quot;买入&quot;</td><td>4.05</td><td>34400.0</td><td>139320.0</td><td>22.29</td><td>0.0</td><td>1.39</td><td>0.0</td><td>-139342.29</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>34400.0</td></tr><tr><td>2022-07-11</td><td>&quot;002462&quot;</td><td>5</td><td>&quot;湘财&quot;</td><td>&quot;09:38:16&quot;</td><td>&quot;嘉事堂&quot;</td><td>&quot;买入&quot;</td><td>13.51</td><td>10400.0</td><td>140504.0</td><td>22.48</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-140526.48</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>10400.0</td></tr><tr><td>2022-07-11</td><td>&quot;600894&quot;</td><td>3</td><td>&quot;湘财&quot;</td><td>&quot;09:36:30&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>21400.0</td></tr><tr><td>2022-07-11</td><td>&quot;601077&quot;</td><td>2</td><td>&quot;湘财&quot;</td><td>&quot;09:34:24&quot;</td><td>&quot;渝农商行&quot;</td><td>&quot;买入&quot;</td><td>3.65</td><td>38300.0</td><td>139795.0</td><td>22.37</td><td>0.0</td><td>1.38</td><td>0.0</td><td>-139818.75</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>38300.0</td></tr><tr><td>2022-07-11</td><td>&quot;601992&quot;</td><td>4</td><td>&quot;湘财&quot;</td><td>&quot;09:37:25&quot;</td><td>&quot;金隅集团&quot;</td><td>&quot;买入&quot;</td><td>2.59</td><td>54000.0</td><td>139860.0</td><td>22.38</td><td>0.0</td><td>1.42</td><td>0.0</td><td>-139883.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>54000.0</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>2023-10-31</td><td>&quot;300132&quot;</td><td>361</td><td>&quot;海通两融&quot;</td><td>&quot;09:40:55&quot;</td><td>&quot;青松股份&quot;</td><td>&quot;买入&quot;</td><td>5.21</td><td>9600.0</td><td>50016.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-50021.0</td><td>0.0001</td><td>0.0</td><td>0.0</td><td>9600.0</td></tr><tr><td>2023-10-31</td><td>&quot;300215&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>20000.0</td></tr><tr><td>2023-10-31</td><td>&quot;600525&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>8500.0</td></tr><tr><td>2023-10-31</td><td>&quot;603214&quot;</td><td>360</td><td>&quot;海通两融&quot;</td><td>&quot;09:39:57&quot;</td><td>&quot;爱婴室&quot;</td><td>&quot;买入&quot;</td><td>15.84</td><td>3100.0</td><td>49104.0</td><td>5.0</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-49109.51</td><td>0.000102</td><td>0.0</td><td>0.00001</td><td>3100.0</td></tr><tr><td>2023-10-31</td><td>&quot;688660&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>20200.0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (4_489, 19)\n",
       "┌─────────┬─────────┬──────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 交易日  ┆ 证券代  ┆ 序号 ┆ 券商    ┆ … ┆ 手续费  ┆ 印花税  ┆ 过户费 ┆ 结余数 │\n",
       "│ 期      ┆ 码      ┆ ---  ┆ ---     ┆   ┆ 率      ┆ 率      ┆ 率     ┆ 量     │\n",
       "│ ---     ┆ ---     ┆ u32  ┆ str     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│ date    ┆ str     ┆      ┆         ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞═════════╪═════════╪══════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 2022-07 ┆ 000900  ┆ 1    ┆ 湘财    ┆ … ┆ 0.00016 ┆ 0.0     ┆ 0.0000 ┆ 34400. │\n",
       "│ -11     ┆         ┆      ┆         ┆   ┆         ┆         ┆ 1      ┆ 0      │\n",
       "│ 2022-07 ┆ 002462  ┆ 5    ┆ 湘财    ┆ … ┆ 0.00016 ┆ 0.0     ┆ 0.0000 ┆ 10400. │\n",
       "│ -11     ┆         ┆      ┆         ┆   ┆         ┆         ┆ 1      ┆ 0      │\n",
       "│ 2022-07 ┆ 600894  ┆ 3    ┆ 湘财    ┆ … ┆ 0.00016 ┆ 0.0     ┆ 0.0000 ┆ 21400. │\n",
       "│ -11     ┆         ┆      ┆         ┆   ┆         ┆         ┆ 1      ┆ 0      │\n",
       "│ 2022-07 ┆ 601077  ┆ 2    ┆ 湘财    ┆ … ┆ 0.00016 ┆ 0.0     ┆ 0.0000 ┆ 38300. │\n",
       "│ -11     ┆         ┆      ┆         ┆   ┆         ┆         ┆ 1      ┆ 0      │\n",
       "│ 2022-07 ┆ 601992  ┆ 4    ┆ 湘财    ┆ … ┆ 0.00016 ┆ 0.0     ┆ 0.0000 ┆ 54000. │\n",
       "│ -11     ┆         ┆      ┆         ┆   ┆         ┆         ┆ 1      ┆ 0      │\n",
       "│ …       ┆ …       ┆ …    ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 2023-10 ┆ 300132  ┆ 361  ┆ 海通两  ┆ … ┆ 0.0001  ┆ 0.0     ┆ 0.0    ┆ 9600.0 │\n",
       "│ -31     ┆         ┆      ┆ 融      ┆   ┆         ┆         ┆        ┆        │\n",
       "│ 2023-10 ┆ 300215  ┆ null ┆ null    ┆ … ┆ null    ┆ null    ┆ null   ┆ 20000. │\n",
       "│ -31     ┆         ┆      ┆         ┆   ┆         ┆         ┆        ┆ 0      │\n",
       "│ 2023-10 ┆ 600525  ┆ null ┆ null    ┆ … ┆ null    ┆ null    ┆ null   ┆ 8500.0 │\n",
       "│ -31     ┆         ┆      ┆         ┆   ┆         ┆         ┆        ┆        │\n",
       "│ 2023-10 ┆ 603214  ┆ 360  ┆ 海通两  ┆ … ┆ 0.00010 ┆ 0.0     ┆ 0.0000 ┆ 3100.0 │\n",
       "│ -31     ┆         ┆      ┆ 融      ┆   ┆ 2       ┆         ┆ 1      ┆        │\n",
       "│ 2023-10 ┆ 688660  ┆ null ┆ null    ┆ … ┆ null    ┆ null    ┆ null   ┆ 20200. │\n",
       "│ -31     ┆         ┆      ┆         ┆   ┆         ┆         ┆        ┆ 0      │\n",
       "└─────────┴─────────┴──────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "fd45c4bc-e583-46c6-8c22-d8358500e496",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts_codes = (\n",
    "    d1.select(\n",
    "        证券代码=(\n",
    "            pl.when(pl.col(\"证券代码\").str.head(1).is_in([\"0\", \"3\"]))\n",
    "            .then(pl.format(\"{}.SZ\", pl.col(\"证券代码\")))\n",
    "            .when(pl.col(\"证券代码\").str.head(1) == \"6\")\n",
    "            .then(pl.format(\"{}.SH\", pl.col(\"证券代码\")))\n",
    "        ),\n",
    "    )\n",
    "    .to_series()\n",
    "    .unique()\n",
    "    .sort()\n",
    "    .to_list()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "6c2bba2e-25ae-4697-bcb2-5c496a1c4dce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['000096.SZ',\n",
       " '000532.SZ',\n",
       " '000559.SZ',\n",
       " '000599.SZ',\n",
       " '000655.SZ',\n",
       " '000698.SZ',\n",
       " '000712.SZ',\n",
       " '000717.SZ',\n",
       " '000751.SZ',\n",
       " '000823.SZ',\n",
       " '000900.SZ',\n",
       " '000903.SZ',\n",
       " '002023.SZ',\n",
       " '002054.SZ',\n",
       " '002055.SZ',\n",
       " '002106.SZ',\n",
       " '002111.SZ',\n",
       " '002136.SZ',\n",
       " '002224.SZ',\n",
       " '002225.SZ',\n",
       " '002228.SZ',\n",
       " '002288.SZ',\n",
       " '002331.SZ',\n",
       " '002381.SZ',\n",
       " '002382.SZ',\n",
       " '002392.SZ',\n",
       " '002440.SZ',\n",
       " '002442.SZ',\n",
       " '002462.SZ',\n",
       " '002479.SZ',\n",
       " '002492.SZ',\n",
       " '002521.SZ',\n",
       " '002616.SZ',\n",
       " '002637.SZ',\n",
       " '002661.SZ',\n",
       " '002672.SZ',\n",
       " '002688.SZ',\n",
       " '002702.SZ',\n",
       " '002734.SZ',\n",
       " '002743.SZ',\n",
       " '002753.SZ',\n",
       " '002842.SZ',\n",
       " '002946.SZ',\n",
       " '002956.SZ',\n",
       " '002996.SZ',\n",
       " '002998.SZ',\n",
       " '300006.SZ',\n",
       " '300016.SZ',\n",
       " '300022.SZ',\n",
       " '300107.SZ',\n",
       " '300132.SZ',\n",
       " '300155.SZ',\n",
       " '300190.SZ',\n",
       " '300214.SZ',\n",
       " '300215.SZ',\n",
       " '300221.SZ',\n",
       " '300241.SZ',\n",
       " '300242.SZ',\n",
       " '300259.SZ',\n",
       " '300279.SZ',\n",
       " '300281.SZ',\n",
       " '300283.SZ',\n",
       " '300304.SZ',\n",
       " '300368.SZ',\n",
       " '300385.SZ',\n",
       " '300422.SZ',\n",
       " '300429.SZ',\n",
       " '300464.SZ',\n",
       " '300473.SZ',\n",
       " '300538.SZ',\n",
       " '300543.SZ',\n",
       " '300641.SZ',\n",
       " '300642.SZ',\n",
       " '300651.SZ',\n",
       " '300701.SZ',\n",
       " '300708.SZ',\n",
       " '300905.SZ',\n",
       " '600015.SH',\n",
       " '600022.SH',\n",
       " '600051.SH',\n",
       " '600097.SH',\n",
       " '600120.SH',\n",
       " '600178.SH',\n",
       " '600231.SH',\n",
       " '600261.SH',\n",
       " '600269.SH',\n",
       " '600273.SH',\n",
       " '600287.SH',\n",
       " '600292.SH',\n",
       " '600300.SH',\n",
       " '600336.SH',\n",
       " '600408.SH',\n",
       " '600429.SH',\n",
       " '600513.SH',\n",
       " '600525.SH',\n",
       " '600590.SH',\n",
       " '600597.SH',\n",
       " '600626.SH',\n",
       " '600638.SH',\n",
       " '600643.SH',\n",
       " '600648.SH',\n",
       " '600727.SH',\n",
       " '600729.SH',\n",
       " '600735.SH',\n",
       " '600793.SH',\n",
       " '600800.SH',\n",
       " '600810.SH',\n",
       " '600827.SH',\n",
       " '600829.SH',\n",
       " '600835.SH',\n",
       " '600839.SH',\n",
       " '600843.SH',\n",
       " '600844.SH',\n",
       " '600894.SH',\n",
       " '600960.SH',\n",
       " '600967.SH',\n",
       " '600980.SH',\n",
       " '601002.SH',\n",
       " '601077.SH',\n",
       " '601166.SH',\n",
       " '601992.SH',\n",
       " '603002.SH',\n",
       " '603111.SH',\n",
       " '603113.SH',\n",
       " '603186.SH',\n",
       " '603214.SH',\n",
       " '603385.SH',\n",
       " '603386.SH',\n",
       " '603577.SH',\n",
       " '603626.SH',\n",
       " '603686.SH',\n",
       " '603739.SH',\n",
       " '603878.SH',\n",
       " '603897.SH',\n",
       " '603967.SH',\n",
       " '603988.SH',\n",
       " '605066.SH',\n",
       " '605158.SH',\n",
       " '605196.SH',\n",
       " '605258.SH',\n",
       " '605288.SH',\n",
       " '688011.SH',\n",
       " '688069.SH',\n",
       " '688165.SH',\n",
       " '688299.SH',\n",
       " '688321.SH',\n",
       " '688360.SH',\n",
       " '688393.SH',\n",
       " '688660.SH']"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts_codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "0dbb89ae-f2c5-42ca-b150-0e97221bb8d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "149"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ts_codes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "20608eb2-d15c-44a1-8518-98ed609d4bb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm.notebook import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "8b15f361-a0bb-4e67-8a8f-1be98f88f68f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cb30fbec852d4ebf80e47f9379967989",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/149 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "hq = [\n",
    "    pl.from_pandas(\n",
    "        pro.daily(\n",
    "            ts_code=ts_code,\n",
    "            start_date=format(start_date, \"%Y%m%d\"),\n",
    "            end_date=format(end_date, \"%Y%m%d\"),\n",
    "        )\n",
    "    )\n",
    "    for ts_code in tqdm(ts_codes)\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "0fc4aa48-234d-47db-bb60-2359610182e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame,\n",
       " polars.dataframe.frame.DataFrame]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[type(i) for i in hq]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "99b3a62c-e882-4048-b38c-33ec641fb3ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "hq = pl.concat(hq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "8f8f4b04-3cd6-4db2-9cc8-c97fead38926",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/c/Users/ASUS/repo/week08\n"
     ]
    }
   ],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "fda838a3-2c93-47f3-814d-b13631faf68f",
   "metadata": {},
   "outputs": [],
   "source": [
    "hq.write_parquet(\"daily.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "37e2bf4f-3de5-4b12-a126-b808a804da25",
   "metadata": {},
   "outputs": [],
   "source": [
    "hq = pl.read_parquet(\"daily.parquet\")\n",
    "\n",
    "hq = hq.with_columns(\n",
    "    pl.col(\"ts_code\").str.head(6),\n",
    "    pl.col(\"trade_date\").str.to_date(\"%Y%m%d\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "59c86934-394b-44cb-a634-7f3c56543d97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (358, 18)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:33:37&quot;</td><td>&quot;000900&quot;</td><td>&quot;现代投资&quot;</td><td>&quot;买入&quot;</td><td>4.05</td><td>34400.0</td><td>139320.0</td><td>22.29</td><td>0.0</td><td>1.39</td><td>0.0</td><td>-139342.29</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>2</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:34:24&quot;</td><td>&quot;601077&quot;</td><td>&quot;渝农商行&quot;</td><td>&quot;买入&quot;</td><td>3.65</td><td>38300.0</td><td>139795.0</td><td>22.37</td><td>0.0</td><td>1.38</td><td>0.0</td><td>-139818.75</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>3</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:36:30&quot;</td><td>&quot;600894&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>4</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:37:25&quot;</td><td>&quot;601992&quot;</td><td>&quot;金隅集团&quot;</td><td>&quot;买入&quot;</td><td>2.59</td><td>54000.0</td><td>139860.0</td><td>22.38</td><td>0.0</td><td>1.42</td><td>0.0</td><td>-139883.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>5</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:38:16&quot;</td><td>&quot;002462&quot;</td><td>&quot;嘉事堂&quot;</td><td>&quot;买入&quot;</td><td>13.51</td><td>10400.0</td><td>140504.0</td><td>22.48</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-140526.48</td><td>0.00016</td><td>0.0</td><td>0.00001</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>359</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:31:53&quot;</td><td>&quot;002956&quot;</td><td>&quot;西麦食品&quot;</td><td>&quot;卖出&quot;</td><td>14.13</td><td>5000.0</td><td>70650.0</td><td>6.74</td><td>35.35</td><td>0.0</td><td>0.0</td><td>70607.91</td><td>0.000095</td><td>0.0005</td><td>0.0</td></tr><tr><td>360</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:39:57&quot;</td><td>&quot;603214&quot;</td><td>&quot;爱婴室&quot;</td><td>&quot;买入&quot;</td><td>15.84</td><td>3100.0</td><td>49104.0</td><td>5.0</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-49109.51</td><td>0.000102</td><td>0.0</td><td>0.00001</td></tr><tr><td>361</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:40:55&quot;</td><td>&quot;300132&quot;</td><td>&quot;青松股份&quot;</td><td>&quot;买入&quot;</td><td>5.21</td><td>9600.0</td><td>50016.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-50021.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr><tr><td>362</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:43:13&quot;</td><td>&quot;002492&quot;</td><td>&quot;恒基达鑫&quot;</td><td>&quot;买入&quot;</td><td>5.91</td><td>8400.0</td><td>49644.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49649.0</td><td>0.000101</td><td>0.0</td><td>0.0</td></tr><tr><td>363</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:44:45&quot;</td><td>&quot;002111&quot;</td><td>&quot;威海广泰&quot;</td><td>&quot;买入&quot;</td><td>9.24</td><td>5400.0</td><td>49896.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49901.0</td><td>0.0001</td><td>0.0</td><td>0.0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (358, 18)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬─────────┬─────────┬────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ 发生金  ┆ 手续费  ┆ 印花税 ┆ 过户费 │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ 额      ┆ 率      ┆ 率     ┆ 率     │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ ---     ┆ ---     ┆ ---    ┆ ---    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆ f64     ┆ f64     ┆ f64    ┆ f64    │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪═════════╪═════════╪════════╪════════╡\n",
       "│ 1    ┆ 湘财    ┆ 2022-07 ┆ 09:33:3 ┆ … ┆ -139342 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 7       ┆   ┆ .29     ┆         ┆        ┆ 1      │\n",
       "│ 2    ┆ 湘财    ┆ 2022-07 ┆ 09:34:2 ┆ … ┆ -139818 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 4       ┆   ┆ .75     ┆         ┆        ┆ 1      │\n",
       "│ 3    ┆ 湘财    ┆ 2022-07 ┆ 09:36:3 ┆ … ┆ -139979 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 0       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 4    ┆ 湘财    ┆ 2022-07 ┆ 09:37:2 ┆ … ┆ -139883 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 5       ┆   ┆ .8      ┆         ┆        ┆ 1      │\n",
       "│ 5    ┆ 湘财    ┆ 2022-07 ┆ 09:38:1 ┆ … ┆ -140526 ┆ 0.00016 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆         ┆ -11     ┆ 6       ┆   ┆ .48     ┆         ┆        ┆ 1      │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …       ┆ …       ┆ …      ┆ …      │\n",
       "│ 359  ┆ 海通两  ┆ 2023-10 ┆ 09:31:5 ┆ … ┆ 70607.9 ┆ 0.00009 ┆ 0.0005 ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 1       ┆ 5       ┆        ┆        │\n",
       "│ 360  ┆ 海通两  ┆ 2023-10 ┆ 09:39:5 ┆ … ┆ -49109. ┆ 0.00010 ┆ 0.0    ┆ 0.0000 │\n",
       "│      ┆ 融      ┆ -31     ┆ 7       ┆   ┆ 51      ┆ 2       ┆        ┆ 1      │\n",
       "│ 361  ┆ 海通两  ┆ 2023-10 ┆ 09:40:5 ┆ … ┆ -50021. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "│ 362  ┆ 海通两  ┆ 2023-10 ┆ 09:43:1 ┆ … ┆ -49649. ┆ 0.00010 ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆ 0       ┆ 1       ┆        ┆        │\n",
       "│ 363  ┆ 海通两  ┆ 2023-10 ┆ 09:44:4 ┆ … ┆ -49901. ┆ 0.0001  ┆ 0.0    ┆ 0.0    │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆ 0       ┆         ┆        ┆        │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴─────────┴─────────┴────────┴────────┘"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "92331efd-8d07-4b62-80da-94ef7c7ca676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (358, 27)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th><th>open</th><th>high</th><th>low</th><th>close</th><th>pre_close</th><th>change</th><th>pct_chg</th><th>vol</th><th>amount</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:33:37&quot;</td><td>&quot;000900&quot;</td><td>&quot;现代投资&quot;</td><td>&quot;买入&quot;</td><td>4.05</td><td>34400.0</td><td>139320.0</td><td>22.29</td><td>0.0</td><td>1.39</td><td>0.0</td><td>-139342.29</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>4.08</td><td>4.13</td><td>4.04</td><td>4.12</td><td>4.06</td><td>0.06</td><td>1.4778</td><td>89030.64</td><td>36352.212</td></tr><tr><td>2</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:34:24&quot;</td><td>&quot;601077&quot;</td><td>&quot;渝农商行&quot;</td><td>&quot;买入&quot;</td><td>3.65</td><td>38300.0</td><td>139795.0</td><td>22.37</td><td>0.0</td><td>1.38</td><td>0.0</td><td>-139818.75</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>3.65</td><td>3.68</td><td>3.64</td><td>3.66</td><td>3.65</td><td>0.01</td><td>0.274</td><td>370994.07</td><td>135739.515</td></tr><tr><td>3</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:36:30&quot;</td><td>&quot;600894&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>6.57</td><td>6.57</td><td>6.49</td><td>6.51</td><td>6.57</td><td>-0.06</td><td>-0.9132</td><td>20737.5</td><td>13537.134</td></tr><tr><td>4</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:37:25&quot;</td><td>&quot;601992&quot;</td><td>&quot;金隅集团&quot;</td><td>&quot;买入&quot;</td><td>2.59</td><td>54000.0</td><td>139860.0</td><td>22.38</td><td>0.0</td><td>1.42</td><td>0.0</td><td>-139883.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>2.61</td><td>2.62</td><td>2.58</td><td>2.59</td><td>2.61</td><td>-0.02</td><td>-0.7663</td><td>247321.0</td><td>64052.01</td></tr><tr><td>5</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:38:16&quot;</td><td>&quot;002462&quot;</td><td>&quot;嘉事堂&quot;</td><td>&quot;买入&quot;</td><td>13.51</td><td>10400.0</td><td>140504.0</td><td>22.48</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-140526.48</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>13.2</td><td>13.96</td><td>13.07</td><td>13.65</td><td>13.18</td><td>0.47</td><td>3.566</td><td>62827.0</td><td>85869.11</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>359</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:31:53&quot;</td><td>&quot;002956&quot;</td><td>&quot;西麦食品&quot;</td><td>&quot;卖出&quot;</td><td>14.13</td><td>5000.0</td><td>70650.0</td><td>6.74</td><td>35.35</td><td>0.0</td><td>0.0</td><td>70607.91</td><td>0.000095</td><td>0.0005</td><td>0.0</td><td>14.2</td><td>14.27</td><td>14.01</td><td>14.07</td><td>14.21</td><td>-0.14</td><td>-0.9852</td><td>17859.2</td><td>25247.849</td></tr><tr><td>360</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:39:57&quot;</td><td>&quot;603214&quot;</td><td>&quot;爱婴室&quot;</td><td>&quot;买入&quot;</td><td>15.84</td><td>3100.0</td><td>49104.0</td><td>5.0</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-49109.51</td><td>0.000102</td><td>0.0</td><td>0.00001</td><td>15.81</td><td>15.93</td><td>15.65</td><td>15.78</td><td>15.8</td><td>-0.02</td><td>-0.1266</td><td>18384.12</td><td>29025.236</td></tr><tr><td>361</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:40:55&quot;</td><td>&quot;300132&quot;</td><td>&quot;青松股份&quot;</td><td>&quot;买入&quot;</td><td>5.21</td><td>9600.0</td><td>50016.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-50021.0</td><td>0.0001</td><td>0.0</td><td>0.0</td><td>5.18</td><td>5.32</td><td>5.17</td><td>5.25</td><td>5.17</td><td>0.08</td><td>1.5474</td><td>88442.01</td><td>46306.236</td></tr><tr><td>362</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:43:13&quot;</td><td>&quot;002492&quot;</td><td>&quot;恒基达鑫&quot;</td><td>&quot;买入&quot;</td><td>5.91</td><td>8400.0</td><td>49644.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49649.0</td><td>0.000101</td><td>0.0</td><td>0.0</td><td>5.87</td><td>5.94</td><td>5.86</td><td>5.91</td><td>5.87</td><td>0.04</td><td>0.6814</td><td>40253.0</td><td>23766.196</td></tr><tr><td>363</td><td>&quot;海通两融&quot;</td><td>2023-10-31</td><td>&quot;09:44:45&quot;</td><td>&quot;002111&quot;</td><td>&quot;威海广泰&quot;</td><td>&quot;买入&quot;</td><td>9.24</td><td>5400.0</td><td>49896.0</td><td>5.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-49901.0</td><td>0.0001</td><td>0.0</td><td>0.0</td><td>9.16</td><td>9.29</td><td>9.09</td><td>9.24</td><td>9.17</td><td>0.07</td><td>0.7634</td><td>43642.92</td><td>40302.885</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (358, 27)\n",
       "┌──────┬─────────┬─────────┬─────────┬───┬────────┬─────────┬─────────┬────────┐\n",
       "│ 序号 ┆ 券商    ┆ 交易日  ┆ 交易时  ┆ … ┆ change ┆ pct_chg ┆ vol     ┆ amount │\n",
       "│ ---  ┆ ---     ┆ 期      ┆ 间      ┆   ┆ ---    ┆ ---     ┆ ---     ┆ ---    │\n",
       "│ u32  ┆ str     ┆ ---     ┆ ---     ┆   ┆ f64    ┆ f64     ┆ f64     ┆ f64    │\n",
       "│      ┆         ┆ date    ┆ str     ┆   ┆        ┆         ┆         ┆        │\n",
       "╞══════╪═════════╪═════════╪═════════╪═══╪════════╪═════════╪═════════╪════════╡\n",
       "│ 1    ┆ 湘财    ┆ 2022-07 ┆ 09:33:3 ┆ … ┆ 0.06   ┆ 1.4778  ┆ 89030.6 ┆ 36352. │\n",
       "│      ┆         ┆ -11     ┆ 7       ┆   ┆        ┆         ┆ 4       ┆ 212    │\n",
       "│ 2    ┆ 湘财    ┆ 2022-07 ┆ 09:34:2 ┆ … ┆ 0.01   ┆ 0.274   ┆ 370994. ┆ 135739 │\n",
       "│      ┆         ┆ -11     ┆ 4       ┆   ┆        ┆         ┆ 07      ┆ .515   │\n",
       "│ 3    ┆ 湘财    ┆ 2022-07 ┆ 09:36:3 ┆ … ┆ -0.06  ┆ -0.9132 ┆ 20737.5 ┆ 13537. │\n",
       "│      ┆         ┆ -11     ┆ 0       ┆   ┆        ┆         ┆         ┆ 134    │\n",
       "│ 4    ┆ 湘财    ┆ 2022-07 ┆ 09:37:2 ┆ … ┆ -0.02  ┆ -0.7663 ┆ 247321. ┆ 64052. │\n",
       "│      ┆         ┆ -11     ┆ 5       ┆   ┆        ┆         ┆ 0       ┆ 01     │\n",
       "│ 5    ┆ 湘财    ┆ 2022-07 ┆ 09:38:1 ┆ … ┆ 0.47   ┆ 3.566   ┆ 62827.0 ┆ 85869. │\n",
       "│      ┆         ┆ -11     ┆ 6       ┆   ┆        ┆         ┆         ┆ 11     │\n",
       "│ …    ┆ …       ┆ …       ┆ …       ┆ … ┆ …      ┆ …       ┆ …       ┆ …      │\n",
       "│ 359  ┆ 海通两  ┆ 2023-10 ┆ 09:31:5 ┆ … ┆ -0.14  ┆ -0.9852 ┆ 17859.2 ┆ 25247. │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆        ┆         ┆         ┆ 849    │\n",
       "│ 360  ┆ 海通两  ┆ 2023-10 ┆ 09:39:5 ┆ … ┆ -0.02  ┆ -0.1266 ┆ 18384.1 ┆ 29025. │\n",
       "│      ┆ 融      ┆ -31     ┆ 7       ┆   ┆        ┆         ┆ 2       ┆ 236    │\n",
       "│ 361  ┆ 海通两  ┆ 2023-10 ┆ 09:40:5 ┆ … ┆ 0.08   ┆ 1.5474  ┆ 88442.0 ┆ 46306. │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆        ┆         ┆ 1       ┆ 236    │\n",
       "│ 362  ┆ 海通两  ┆ 2023-10 ┆ 09:43:1 ┆ … ┆ 0.04   ┆ 0.6814  ┆ 40253.0 ┆ 23766. │\n",
       "│      ┆ 融      ┆ -31     ┆ 3       ┆   ┆        ┆         ┆         ┆ 196    │\n",
       "│ 363  ┆ 海通两  ┆ 2023-10 ┆ 09:44:4 ┆ … ┆ 0.07   ┆ 0.7634  ┆ 43642.9 ┆ 40302. │\n",
       "│      ┆ 融      ┆ -31     ┆ 5       ┆   ┆        ┆         ┆ 2       ┆ 885    │\n",
       "└──────┴─────────┴─────────┴─────────┴───┴────────┴─────────┴─────────┴────────┘"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1.join(\n",
    "    hq, left_on=[\"交易日期\", \"证券代码\"], right_on=[\"trade_date\", \"ts_code\"], how=\"left\"\n",
    ").filter(\n",
    "    pl.col(\"成交价格\").is_between(pl.col(\"low\"), pl.col(\"high\")),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "a8605a7d-e386-48a4-aea2-46ca4ec92a0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (358, 28)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>序号</th><th>券商</th><th>交易日期</th><th>交易时间</th><th>证券代码</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th><th>open</th><th>high</th><th>low</th><th>close</th><th>pre_close</th><th>change</th><th>pct_chg</th><th>vol</th><th>amount</th><th>vratio</th></tr><tr><td>u32</td><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>237</td><td>&quot;湘财&quot;</td><td>2023-05-09</td><td>&quot;09:33:16&quot;</td><td>&quot;601166&quot;</td><td>&quot;兴业银行&quot;</td><td>&quot;卖出&quot;</td><td>17.75</td><td>3000.0</td><td>53250.0</td><td>8.52</td><td>53.25</td><td>0.53</td><td>0.0</td><td>53187.7</td><td>0.00016</td><td>0.001</td><td>0.00001</td><td>17.77</td><td>17.94</td><td>17.5</td><td>17.56</td><td>17.63</td><td>-0.07</td><td>-0.3971</td><td>1.6276e6</td><td>2.8810e6</td><td>0.000018</td></tr><tr><td>48</td><td>&quot;湘财&quot;</td><td>2022-10-25</td><td>&quot;09:30:19&quot;</td><td>&quot;300368&quot;</td><td>&quot;汇金股份&quot;</td><td>&quot;卖出&quot;</td><td>6.2</td><td>100.0</td><td>620.0</td><td>0.1</td><td>0.62</td><td>0.01</td><td>0.0</td><td>619.28</td><td>0.000161</td><td>0.001</td><td>0.000016</td><td>6.18</td><td>6.25</td><td>5.95</td><td>6.06</td><td>6.2</td><td>-0.14</td><td>-2.2581</td><td>49219.99</td><td>29911.915</td><td>0.00002</td></tr><tr><td>49</td><td>&quot;湘财&quot;</td><td>2022-10-25</td><td>&quot;09:30:39&quot;</td><td>&quot;002996&quot;</td><td>&quot;顺博合金&quot;</td><td>&quot;卖出&quot;</td><td>13.64</td><td>100.0</td><td>1364.0</td><td>0.22</td><td>1.36</td><td>0.01</td><td>0.0</td><td>1362.42</td><td>0.000161</td><td>0.000997</td><td>0.000007</td><td>13.57</td><td>13.93</td><td>13.26</td><td>13.82</td><td>13.64</td><td>0.18</td><td>1.3196</td><td>38258.22</td><td>52122.119</td><td>0.000026</td></tr><tr><td>235</td><td>&quot;湘财&quot;</td><td>2023-04-25</td><td>&quot;13:35:07&quot;</td><td>&quot;601166&quot;</td><td>&quot;兴业银行&quot;</td><td>&quot;买入&quot;</td><td>17.15</td><td>3000.0</td><td>51450.0</td><td>8.23</td><td>0.0</td><td>0.51</td><td>0.0</td><td>-51458.74</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>16.96</td><td>17.29</td><td>16.96</td><td>17.27</td><td>16.93</td><td>0.34</td><td>2.0083</td><td>995234.22</td><td>1.7071e6</td><td>0.00003</td></tr><tr><td>46</td><td>&quot;湘财&quot;</td><td>2022-10-25</td><td>&quot;09:25:00&quot;</td><td>&quot;002998&quot;</td><td>&quot;优彩资源&quot;</td><td>&quot;卖出&quot;</td><td>6.73</td><td>100.0</td><td>673.0</td><td>0.11</td><td>0.67</td><td>0.01</td><td>0.0</td><td>672.22</td><td>0.000163</td><td>0.000996</td><td>0.000015</td><td>6.73</td><td>6.73</td><td>6.56</td><td>6.64</td><td>6.72</td><td>-0.08</td><td>-1.1905</td><td>16145.0</td><td>10687.614</td><td>0.000062</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>30</td><td>&quot;湘财&quot;</td><td>2022-10-11</td><td>&quot;09:41:06&quot;</td><td>&quot;600231&quot;</td><td>&quot;凌钢股份&quot;</td><td>&quot;买入&quot;</td><td>2.2</td><td>62700.0</td><td>137940.0</td><td>22.07</td><td>0.0</td><td>1.29</td><td>0.0</td><td>-137963.36</td><td>0.00016</td><td>0.0</td><td>0.000009</td><td>2.21</td><td>2.22</td><td>2.15</td><td>2.2</td><td>2.21</td><td>-0.01</td><td>-0.4525</td><td>61371.2</td><td>13386.328</td><td>0.010217</td></tr><tr><td>3</td><td>&quot;湘财&quot;</td><td>2022-07-11</td><td>&quot;09:36:30&quot;</td><td>&quot;600894&quot;</td><td>&quot;广日股份&quot;</td><td>&quot;买入&quot;</td><td>6.54</td><td>21400.0</td><td>139956.0</td><td>22.39</td><td>0.0</td><td>1.41</td><td>0.0</td><td>-139979.8</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>6.57</td><td>6.57</td><td>6.49</td><td>6.51</td><td>6.57</td><td>-0.06</td><td>-0.9132</td><td>20737.5</td><td>13537.134</td><td>0.010319</td></tr><tr><td>142</td><td>&quot;湘财&quot;</td><td>2023-02-21</td><td>&quot;09:37:16&quot;</td><td>&quot;300385&quot;</td><td>&quot;雪浪环境&quot;</td><td>&quot;买入&quot;</td><td>6.5293</td><td>11500.0</td><td>75087.0</td><td>12.01</td><td>0.0</td><td>0.77</td><td>0.0</td><td>-75099.01</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>6.53</td><td>6.59</td><td>6.45</td><td>6.52</td><td>6.5</td><td>0.02</td><td>0.3077</td><td>11136.0</td><td>7250.686</td><td>0.010327</td></tr><tr><td>152</td><td>&quot;湘财&quot;</td><td>2023-03-01</td><td>&quot;09:31:46&quot;</td><td>&quot;688069&quot;</td><td>&quot;德林海环保&quot;</td><td>&quot;卖出&quot;</td><td>28.2838</td><td>2600.0</td><td>73538.0</td><td>11.77</td><td>73.58</td><td>0.77</td><td>0.0</td><td>73451.88</td><td>0.00016</td><td>0.001001</td><td>0.00001</td><td>28.52</td><td>28.86</td><td>28.27</td><td>28.67</td><td>28.54</td><td>0.13</td><td>0.4555</td><td>2416.06</td><td>6897.976</td><td>0.010761</td></tr><tr><td>27</td><td>&quot;湘财&quot;</td><td>2022-10-11</td><td>&quot;09:37:32&quot;</td><td>&quot;600261&quot;</td><td>&quot;阳光照明&quot;</td><td>&quot;买入&quot;</td><td>3.1134</td><td>44600.0</td><td>138857.0</td><td>22.22</td><td>0.0</td><td>1.33</td><td>0.0</td><td>-138880.55</td><td>0.00016</td><td>0.0</td><td>0.00001</td><td>3.14</td><td>3.15</td><td>3.06</td><td>3.13</td><td>3.11</td><td>0.02</td><td>0.6431</td><td>41397.03</td><td>12872.499</td><td>0.010774</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (358, 28)\n",
       "┌──────┬──────┬──────────┬─────────┬───┬─────────┬─────────┬─────────┬─────────┐\n",
       "│ 序号 ┆ 券商 ┆ 交易日期 ┆ 交易时  ┆ … ┆ pct_chg ┆ vol     ┆ amount  ┆ vratio  │\n",
       "│ ---  ┆ ---  ┆ ---      ┆ 间      ┆   ┆ ---     ┆ ---     ┆ ---     ┆ ---     │\n",
       "│ u32  ┆ str  ┆ date     ┆ ---     ┆   ┆ f64     ┆ f64     ┆ f64     ┆ f64     │\n",
       "│      ┆      ┆          ┆ str     ┆   ┆         ┆         ┆         ┆         │\n",
       "╞══════╪══════╪══════════╪═════════╪═══╪═════════╪═════════╪═════════╪═════════╡\n",
       "│ 237  ┆ 湘财 ┆ 2023-05- ┆ 09:33:1 ┆ … ┆ -0.3971 ┆ 1.6276e ┆ 2.8810e ┆ 0.00001 │\n",
       "│      ┆      ┆ 09       ┆ 6       ┆   ┆         ┆ 6       ┆ 6       ┆ 8       │\n",
       "│ 48   ┆ 湘财 ┆ 2022-10- ┆ 09:30:1 ┆ … ┆ -2.2581 ┆ 49219.9 ┆ 29911.9 ┆ 0.00002 │\n",
       "│      ┆      ┆ 25       ┆ 9       ┆   ┆         ┆ 9       ┆ 15      ┆         │\n",
       "│ 49   ┆ 湘财 ┆ 2022-10- ┆ 09:30:3 ┆ … ┆ 1.3196  ┆ 38258.2 ┆ 52122.1 ┆ 0.00002 │\n",
       "│      ┆      ┆ 25       ┆ 9       ┆   ┆         ┆ 2       ┆ 19      ┆ 6       │\n",
       "│ 235  ┆ 湘财 ┆ 2023-04- ┆ 13:35:0 ┆ … ┆ 2.0083  ┆ 995234. ┆ 1.7071e ┆ 0.00003 │\n",
       "│      ┆      ┆ 25       ┆ 7       ┆   ┆         ┆ 22      ┆ 6       ┆         │\n",
       "│ 46   ┆ 湘财 ┆ 2022-10- ┆ 09:25:0 ┆ … ┆ -1.1905 ┆ 16145.0 ┆ 10687.6 ┆ 0.00006 │\n",
       "│      ┆      ┆ 25       ┆ 0       ┆   ┆         ┆         ┆ 14      ┆ 2       │\n",
       "│ …    ┆ …    ┆ …        ┆ …       ┆ … ┆ …       ┆ …       ┆ …       ┆ …       │\n",
       "│ 30   ┆ 湘财 ┆ 2022-10- ┆ 09:41:0 ┆ … ┆ -0.4525 ┆ 61371.2 ┆ 13386.3 ┆ 0.01021 │\n",
       "│      ┆      ┆ 11       ┆ 6       ┆   ┆         ┆         ┆ 28      ┆ 7       │\n",
       "│ 3    ┆ 湘财 ┆ 2022-07- ┆ 09:36:3 ┆ … ┆ -0.9132 ┆ 20737.5 ┆ 13537.1 ┆ 0.01031 │\n",
       "│      ┆      ┆ 11       ┆ 0       ┆   ┆         ┆         ┆ 34      ┆ 9       │\n",
       "│ 142  ┆ 湘财 ┆ 2023-02- ┆ 09:37:1 ┆ … ┆ 0.3077  ┆ 11136.0 ┆ 7250.68 ┆ 0.01032 │\n",
       "│      ┆      ┆ 21       ┆ 6       ┆   ┆         ┆         ┆ 6       ┆ 7       │\n",
       "│ 152  ┆ 湘财 ┆ 2023-03- ┆ 09:31:4 ┆ … ┆ 0.4555  ┆ 2416.06 ┆ 6897.97 ┆ 0.01076 │\n",
       "│      ┆      ┆ 01       ┆ 6       ┆   ┆         ┆         ┆ 6       ┆ 1       │\n",
       "│ 27   ┆ 湘财 ┆ 2022-10- ┆ 09:37:3 ┆ … ┆ 0.6431  ┆ 41397.0 ┆ 12872.4 ┆ 0.01077 │\n",
       "│      ┆      ┆ 11       ┆ 2       ┆   ┆         ┆ 3       ┆ 99      ┆ 4       │\n",
       "└──────┴──────┴──────────┴─────────┴───┴─────────┴─────────┴─────────┴─────────┘"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1.join(\n",
    "    hq, left_on=[\"交易日期\", \"证券代码\"], right_on=[\"trade_date\", \"ts_code\"], how=\"left\"\n",
    ").with_columns(\n",
    "    vratio=pl.col(\"成交数量\") / 100 / pl.col(\"vol\"),\n",
    ").sort(\"vratio\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "ebf5a5d4-568b-41f1-a767-7d5d106c3f60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (72_671, 19)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>交易日期</th><th>证券代码</th><th>序号</th><th>券商</th><th>交易时间</th><th>证券名称</th><th>买卖标志</th><th>成交价格</th><th>成交数量</th><th>成交金额</th><th>手续费</th><th>印花税</th><th>过户费</th><th>其他费</th><th>发生金额</th><th>手续费率</th><th>印花税率</th><th>过户费率</th><th>结余数量</th></tr><tr><td>date</td><td>str</td><td>u32</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>2022-07-11</td><td>&quot;000096&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2022-07-11</td><td>&quot;000532&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2022-07-11</td><td>&quot;000559&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2022-07-11</td><td>&quot;000599&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2022-07-11</td><td>&quot;000655&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>2023-10-31</td><td>&quot;688299&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2023-10-31</td><td>&quot;688321&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2023-10-31</td><td>&quot;688360&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2023-10-31</td><td>&quot;688393&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>0.0</td></tr><tr><td>2023-10-31</td><td>&quot;688660&quot;</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>20200.0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (72_671, 19)\n",
       "┌──────────┬─────────┬──────┬──────┬───┬─────────┬─────────┬─────────┬─────────┐\n",
       "│ 交易日期 ┆ 证券代  ┆ 序号 ┆ 券商 ┆ … ┆ 手续费  ┆ 印花税  ┆ 过户费  ┆ 结余数  │\n",
       "│ ---      ┆ 码      ┆ ---  ┆ ---  ┆   ┆ 率      ┆ 率      ┆ 率      ┆ 量      │\n",
       "│ date     ┆ ---     ┆ u32  ┆ str  ┆   ┆ ---     ┆ ---     ┆ ---     ┆ ---     │\n",
       "│          ┆ str     ┆      ┆      ┆   ┆ f64     ┆ f64     ┆ f64     ┆ f64     │\n",
       "╞══════════╪═════════╪══════╪══════╪═══╪═════════╪═════════╪═════════╪═════════╡\n",
       "│ 2022-07- ┆ 000096  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 11       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2022-07- ┆ 000532  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 11       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2022-07- ┆ 000559  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 11       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2022-07- ┆ 000599  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 11       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2022-07- ┆ 000655  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 11       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ …        ┆ …       ┆ …    ┆ …    ┆ … ┆ …       ┆ …       ┆ …       ┆ …       │\n",
       "│ 2023-10- ┆ 688299  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 31       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2023-10- ┆ 688321  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 31       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2023-10- ┆ 688360  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 31       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2023-10- ┆ 688393  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 0.0     │\n",
       "│ 31       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "│ 2023-10- ┆ 688660  ┆ null ┆ null ┆ … ┆ null    ┆ null    ┆ null    ┆ 20200.0 │\n",
       "│ 31       ┆         ┆      ┆      ┆   ┆         ┆         ┆         ┆         │\n",
       "└──────────┴─────────┴──────┴──────┴───┴─────────┴─────────┴─────────┴─────────┘"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3 = (\n",
    "    k.join(\n",
    "        d1,\n",
    "        left_on=[\"交易日期\", \"证券代码\"],\n",
    "        right_on=[\"交易日期\", \"证券代码\"],\n",
    "        how=\"left\",\n",
    "    )\n",
    "    .sort(\"交易日期\", \"证券代码\")\n",
    "    .with_columns(\n",
    "        结余数量=(\n",
    "            pl.when(pl.col(\"买卖标志\") == \"卖出\")\n",
    "            .then(-pl.col(\"成交数量\"))\n",
    "            .when(pl.col(\"买卖标志\") == \"买入\")\n",
    "            .then(pl.col(\"成交数量\"))\n",
    "            .otherwise(0)\n",
    "            .cum_sum()\n",
    "            .over(\"证券代码\")\n",
    "        ),\n",
    "    )\n",
    ")\n",
    "d3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "a2825bc9-2858-416c-b05f-6ab0cba6aba0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (7, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>a</th><th>b</th></tr><tr><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>2</td><td>2</td></tr><tr><td>8</td><td>8</td></tr><tr><td>null</td><td>1</td></tr><tr><td>1</td><td>1</td></tr><tr><td>null</td><td>9</td></tr><tr><td>9</td><td>9</td></tr><tr><td>null</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (7, 2)\n",
       "┌──────┬──────┐\n",
       "│ a    ┆ b    │\n",
       "│ ---  ┆ ---  │\n",
       "│ i64  ┆ i64  │\n",
       "╞══════╪══════╡\n",
       "│ 2    ┆ 2    │\n",
       "│ 8    ┆ 8    │\n",
       "│ null ┆ 1    │\n",
       "│ 1    ┆ 1    │\n",
       "│ null ┆ 9    │\n",
       "│ 9    ┆ 9    │\n",
       "│ null ┆ null │\n",
       "└──────┴──────┘"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pl.DataFrame({\"a\": [2, 8, None, 1, None, 9, None]}).with_columns(\n",
    "    b=pl.col.a.fill_null(strategy=\"backward\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "f398ba8e-20b4-46ef-9221-9cc0ee8aad74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (47_355, 11)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ts_code</th><th>trade_date</th><th>open</th><th>high</th><th>low</th><th>close</th><th>pre_close</th><th>change</th><th>pct_chg</th><th>vol</th><th>amount</th></tr><tr><td>str</td><td>date</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>&quot;000096&quot;</td><td>2023-10-31</td><td>8.95</td><td>9.25</td><td>8.88</td><td>8.95</td><td>8.84</td><td>0.11</td><td>1.2443</td><td>45191.22</td><td>40697.722</td></tr><tr><td>&quot;000096&quot;</td><td>2023-10-30</td><td>8.72</td><td>8.94</td><td>8.68</td><td>8.84</td><td>8.78</td><td>0.06</td><td>0.6834</td><td>33567.85</td><td>29716.156</td></tr><tr><td>&quot;000096&quot;</td><td>2023-10-27</td><td>8.97</td><td>8.98</td><td>8.75</td><td>8.78</td><td>8.94</td><td>-0.16</td><td>-1.7897</td><td>30035.5</td><td>26547.822</td></tr><tr><td>&quot;000096&quot;</td><td>2023-10-26</td><td>9.12</td><td>9.13</td><td>8.83</td><td>8.94</td><td>9.06</td><td>-0.12</td><td>-1.3245</td><td>28794.0</td><td>25735.179</td></tr><tr><td>&quot;000096&quot;</td><td>2023-10-25</td><td>9.04</td><td>9.2</td><td>8.92</td><td>9.06</td><td>8.99</td><td>0.07</td><td>0.7786</td><td>31960.0</td><td>29019.019</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;688660&quot;</td><td>2022-07-15</td><td>9.73</td><td>10.11</td><td>9.65</td><td>9.76</td><td>9.73</td><td>0.03</td><td>0.3083</td><td>338386.93</td><td>334715.635</td></tr><tr><td>&quot;688660&quot;</td><td>2022-07-14</td><td>9.67</td><td>9.84</td><td>9.55</td><td>9.73</td><td>9.67</td><td>0.06</td><td>0.6205</td><td>329943.3</td><td>319289.544</td></tr><tr><td>&quot;688660&quot;</td><td>2022-07-13</td><td>9.06</td><td>9.86</td><td>9.0</td><td>9.67</td><td>9.06</td><td>0.61</td><td>6.7329</td><td>550236.58</td><td>524088.481</td></tr><tr><td>&quot;688660&quot;</td><td>2022-07-12</td><td>8.99</td><td>9.39</td><td>8.89</td><td>9.06</td><td>9.04</td><td>0.02</td><td>0.2212</td><td>288562.46</td><td>264062.174</td></tr><tr><td>&quot;688660&quot;</td><td>2022-07-11</td><td>9.0</td><td>9.2</td><td>8.89</td><td>9.04</td><td>9.02</td><td>0.02</td><td>0.2217</td><td>166819.63</td><td>150747.732</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (47_355, 11)\n",
       "┌─────────┬──────────┬──────┬───────┬───┬────────┬─────────┬─────────┬─────────┐\n",
       "│ ts_code ┆ trade_da ┆ open ┆ high  ┆ … ┆ change ┆ pct_chg ┆ vol     ┆ amount  │\n",
       "│ ---     ┆ te       ┆ ---  ┆ ---   ┆   ┆ ---    ┆ ---     ┆ ---     ┆ ---     │\n",
       "│ str     ┆ ---      ┆ f64  ┆ f64   ┆   ┆ f64    ┆ f64     ┆ f64     ┆ f64     │\n",
       "│         ┆ date     ┆      ┆       ┆   ┆        ┆         ┆         ┆         │\n",
       "╞═════════╪══════════╪══════╪═══════╪═══╪════════╪═════════╪═════════╪═════════╡\n",
       "│ 000096  ┆ 2023-10- ┆ 8.95 ┆ 9.25  ┆ … ┆ 0.11   ┆ 1.2443  ┆ 45191.2 ┆ 40697.7 │\n",
       "│         ┆ 31       ┆      ┆       ┆   ┆        ┆         ┆ 2       ┆ 22      │\n",
       "│ 000096  ┆ 2023-10- ┆ 8.72 ┆ 8.94  ┆ … ┆ 0.06   ┆ 0.6834  ┆ 33567.8 ┆ 29716.1 │\n",
       "│         ┆ 30       ┆      ┆       ┆   ┆        ┆         ┆ 5       ┆ 56      │\n",
       "│ 000096  ┆ 2023-10- ┆ 8.97 ┆ 8.98  ┆ … ┆ -0.16  ┆ -1.7897 ┆ 30035.5 ┆ 26547.8 │\n",
       "│         ┆ 27       ┆      ┆       ┆   ┆        ┆         ┆         ┆ 22      │\n",
       "│ 000096  ┆ 2023-10- ┆ 9.12 ┆ 9.13  ┆ … ┆ -0.12  ┆ -1.3245 ┆ 28794.0 ┆ 25735.1 │\n",
       "│         ┆ 26       ┆      ┆       ┆   ┆        ┆         ┆         ┆ 79      │\n",
       "│ 000096  ┆ 2023-10- ┆ 9.04 ┆ 9.2   ┆ … ┆ 0.07   ┆ 0.7786  ┆ 31960.0 ┆ 29019.0 │\n",
       "│         ┆ 25       ┆      ┆       ┆   ┆        ┆         ┆         ┆ 19      │\n",
       "│ …       ┆ …        ┆ …    ┆ …     ┆ … ┆ …      ┆ …       ┆ …       ┆ …       │\n",
       "│ 688660  ┆ 2022-07- ┆ 9.73 ┆ 10.11 ┆ … ┆ 0.03   ┆ 0.3083  ┆ 338386. ┆ 334715. │\n",
       "│         ┆ 15       ┆      ┆       ┆   ┆        ┆         ┆ 93      ┆ 635     │\n",
       "│ 688660  ┆ 2022-07- ┆ 9.67 ┆ 9.84  ┆ … ┆ 0.06   ┆ 0.6205  ┆ 329943. ┆ 319289. │\n",
       "│         ┆ 14       ┆      ┆       ┆   ┆        ┆         ┆ 3       ┆ 544     │\n",
       "│ 688660  ┆ 2022-07- ┆ 9.06 ┆ 9.86  ┆ … ┆ 0.61   ┆ 6.7329  ┆ 550236. ┆ 524088. │\n",
       "│         ┆ 13       ┆      ┆       ┆   ┆        ┆         ┆ 58      ┆ 481     │\n",
       "│ 688660  ┆ 2022-07- ┆ 8.99 ┆ 9.39  ┆ … ┆ 0.02   ┆ 0.2212  ┆ 288562. ┆ 264062. │\n",
       "│         ┆ 12       ┆      ┆       ┆   ┆        ┆         ┆ 46      ┆ 174     │\n",
       "│ 688660  ┆ 2022-07- ┆ 9.0  ┆ 9.2   ┆ … ┆ 0.02   ┆ 0.2217  ┆ 166819. ┆ 150747. │\n",
       "│         ┆ 11       ┆      ┆       ┆   ┆        ┆         ┆ 63      ┆ 732     │\n",
       "└─────────┴──────────┴──────┴───────┴───┴────────┴─────────┴─────────┴─────────┘"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "0b484d36-58a9-41dc-95cc-0c6107fb9be0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (478, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>交易日期</th><th>持股市值</th><th>发生金额</th><th>转账金额</th><th>现金余额</th><th>总资产</th></tr><tr><td>date</td><td>f64</td><td>f64</td><td>i32</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>2022-07-11</td><td>703040.0</td><td>-699551.12</td><td>1000000</td><td>300448.88</td><td>1.0035e6</td></tr><tr><td>2022-07-12</td><td>707714.0</td><td>0.0</td><td>0</td><td>300448.88</td><td>1.0082e6</td></tr><tr><td>2022-07-13</td><td>713855.0</td><td>0.0</td><td>0</td><td>300448.88</td><td>1.0143e6</td></tr><tr><td>2022-07-14</td><td>710079.0</td><td>0.0</td><td>0</td><td>300448.88</td><td>1.0105e6</td></tr><tr><td>2022-07-15</td><td>692377.0</td><td>0.0</td><td>0</td><td>300448.88</td><td>992825.88</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>2023-10-27</td><td>571195.0</td><td>0.0</td><td>0</td><td>510845.96</td><td>1.0820e6</td></tr><tr><td>2023-10-28</td><td>571195.0</td><td>0.0</td><td>0</td><td>510845.96</td><td>1.0820e6</td></tr><tr><td>2023-10-29</td><td>571195.0</td><td>0.0</td><td>0</td><td>510845.96</td><td>1.0820e6</td></tr><tr><td>2023-10-30</td><td>686345.0</td><td>-94884.93</td><td>0</td><td>415961.03</td><td>1.1023e6</td></tr><tr><td>2023-10-31</td><td>748947.0</td><td>6490.78</td><td>0</td><td>422451.81</td><td>1.1714e6</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (478, 6)\n",
       "┌────────────┬──────────┬────────────┬──────────┬───────────┬───────────┐\n",
       "│ 交易日期   ┆ 持股市值 ┆ 发生金额   ┆ 转账金额 ┆ 现金余额  ┆ 总资产    │\n",
       "│ ---        ┆ ---      ┆ ---        ┆ ---      ┆ ---       ┆ ---       │\n",
       "│ date       ┆ f64      ┆ f64        ┆ i32      ┆ f64       ┆ f64       │\n",
       "╞════════════╪══════════╪════════════╪══════════╪═══════════╪═══════════╡\n",
       "│ 2022-07-11 ┆ 703040.0 ┆ -699551.12 ┆ 1000000  ┆ 300448.88 ┆ 1.0035e6  │\n",
       "│ 2022-07-12 ┆ 707714.0 ┆ 0.0        ┆ 0        ┆ 300448.88 ┆ 1.0082e6  │\n",
       "│ 2022-07-13 ┆ 713855.0 ┆ 0.0        ┆ 0        ┆ 300448.88 ┆ 1.0143e6  │\n",
       "│ 2022-07-14 ┆ 710079.0 ┆ 0.0        ┆ 0        ┆ 300448.88 ┆ 1.0105e6  │\n",
       "│ 2022-07-15 ┆ 692377.0 ┆ 0.0        ┆ 0        ┆ 300448.88 ┆ 992825.88 │\n",
       "│ …          ┆ …        ┆ …          ┆ …        ┆ …         ┆ …         │\n",
       "│ 2023-10-27 ┆ 571195.0 ┆ 0.0        ┆ 0        ┆ 510845.96 ┆ 1.0820e6  │\n",
       "│ 2023-10-28 ┆ 571195.0 ┆ 0.0        ┆ 0        ┆ 510845.96 ┆ 1.0820e6  │\n",
       "│ 2023-10-29 ┆ 571195.0 ┆ 0.0        ┆ 0        ┆ 510845.96 ┆ 1.0820e6  │\n",
       "│ 2023-10-30 ┆ 686345.0 ┆ -94884.93  ┆ 0        ┆ 415961.03 ┆ 1.1023e6  │\n",
       "│ 2023-10-31 ┆ 748947.0 ┆ 6490.78    ┆ 0        ┆ 422451.81 ┆ 1.1714e6  │\n",
       "└────────────┴──────────┴────────────┴──────────┴───────────┴───────────┘"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d4 = (\n",
    "    d3.join(\n",
    "        hq,\n",
    "        left_on=[\"交易日期\", \"证券代码\"],\n",
    "        right_on=[\"trade_date\", \"ts_code\"],\n",
    "        how=\"left\",\n",
    "    )\n",
    "    .sort(\"证券代码\", \"交易日期\")\n",
    "    .with_columns(close=pl.col(\"close\").fill_null(strategy=\"forward\").over(\"证券代码\"))\n",
    "    .with_columns(\n",
    "        持股市值=pl.col(\"结余数量\") * pl.col(\"close\"),\n",
    "    )\n",
    "    .group_by(\"交易日期\")\n",
    "    .agg(\n",
    "        pl.col(\"持股市值\").sum(),\n",
    "        pl.col(\"发生金额\").sum(),\n",
    "    )\n",
    "    .sort(\"交易日期\")\n",
    "    .with_columns(\n",
    "        转账金额=pl.when(pl.int_range(0, pl.len()) == 0).then(100_0000).otherwise(0),\n",
    "    )\n",
    "    .with_columns(\n",
    "        现金余额=(pl.col(\"转账金额\") + pl.col(\"发生金额\")).cum_sum(),\n",
    "    )\n",
    "    .with_columns(总资产=pl.col(\"持股市值\") + pl.col(\"现金余额\"))\n",
    ")\n",
    "d4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "99e95a71-618f-49f0-98ad-f58776c9f10e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f82afccd652e4710b0c05da5bc969b90",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "PerspectiveWidget(binding_mode='server', columns=['交易日期', '持股市值', '发生金额', '转账金额', '现金余额', '总资产'], table_name='…"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PerspectiveWidget(d4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "30e56681-b66d-43c8-918a-a82b692b59e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "ihq = pro.index_daily(\n",
    "    ts_code=\"000300.SH\",\n",
    "    start_date=format(start_date, \"%Y%m%d\"),\n",
    "    end_date=format(end_date, \"%Y%m%d\"),\n",
    "    fields=\"ts_code,trade_date,pct_chg\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "feb13aec-2f34-4a27-9b9f-43b07ec74c78",
   "metadata": {},
   "outputs": [],
   "source": [
    "pl.from_pandas(ihq).write_parquet(\"index_daily.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "fbf3ce55-52c1-4718-a931-c30a7fb5dff3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (318, 5)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ts_code</th><th>trade_date</th><th>pct_chg</th><th>car</th><th>沪深300</th></tr><tr><td>str</td><td>date</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>&quot;000300.SH&quot;</td><td>2022-07-11</td><td>0.983254</td><td>0.983254</td><td>983254.0</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2022-07-12</td><td>0.990585</td><td>0.973997</td><td>973996.66359</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2022-07-13</td><td>1.001818</td><td>0.975767</td><td>975767.389524</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2022-07-14</td><td>1.000142</td><td>0.975906</td><td>975905.948494</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2022-07-15</td><td>0.982983</td><td>0.959299</td><td>959298.956968</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2023-10-25</td><td>1.004969</td><td>0.791288</td><td>791288.453778</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2023-10-26</td><td>1.002764</td><td>0.793476</td><td>793475.575065</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2023-10-27</td><td>1.013727</td><td>0.804368</td><td>804367.614284</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2023-10-30</td><td>1.006003</td><td>0.809196</td><td>809196.233072</td></tr><tr><td>&quot;000300.SH&quot;</td><td>2023-10-31</td><td>0.996856</td><td>0.806652</td><td>806652.120115</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (318, 5)\n",
       "┌───────────┬────────────┬──────────┬──────────┬───────────────┐\n",
       "│ ts_code   ┆ trade_date ┆ pct_chg  ┆ car      ┆ 沪深300       │\n",
       "│ ---       ┆ ---        ┆ ---      ┆ ---      ┆ ---           │\n",
       "│ str       ┆ date       ┆ f64      ┆ f64      ┆ f64           │\n",
       "╞═══════════╪════════════╪══════════╪══════════╪═══════════════╡\n",
       "│ 000300.SH ┆ 2022-07-11 ┆ 0.983254 ┆ 0.983254 ┆ 983254.0      │\n",
       "│ 000300.SH ┆ 2022-07-12 ┆ 0.990585 ┆ 0.973997 ┆ 973996.66359  │\n",
       "│ 000300.SH ┆ 2022-07-13 ┆ 1.001818 ┆ 0.975767 ┆ 975767.389524 │\n",
       "│ 000300.SH ┆ 2022-07-14 ┆ 1.000142 ┆ 0.975906 ┆ 975905.948494 │\n",
       "│ 000300.SH ┆ 2022-07-15 ┆ 0.982983 ┆ 0.959299 ┆ 959298.956968 │\n",
       "│ …         ┆ …          ┆ …        ┆ …        ┆ …             │\n",
       "│ 000300.SH ┆ 2023-10-25 ┆ 1.004969 ┆ 0.791288 ┆ 791288.453778 │\n",
       "│ 000300.SH ┆ 2023-10-26 ┆ 1.002764 ┆ 0.793476 ┆ 793475.575065 │\n",
       "│ 000300.SH ┆ 2023-10-27 ┆ 1.013727 ┆ 0.804368 ┆ 804367.614284 │\n",
       "│ 000300.SH ┆ 2023-10-30 ┆ 1.006003 ┆ 0.809196 ┆ 809196.233072 │\n",
       "│ 000300.SH ┆ 2023-10-31 ┆ 0.996856 ┆ 0.806652 ┆ 806652.120115 │\n",
       "└───────────┴────────────┴──────────┴──────────┴───────────────┘"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ihq = pl.read_parquet(\"index_daily.parquet\")\n",
    "ihq = (\n",
    "    ihq.with_columns(\n",
    "        pl.col(\"trade_date\").str.to_date(\"%Y%m%d\"),\n",
    "        pl.col(\"pct_chg\") / 100 + 1,\n",
    "    )\n",
    "    .sort(\"trade_date\")\n",
    "    .with_columns(\n",
    "        car=pl.col(\"pct_chg\").cum_prod(),\n",
    "    )\n",
    "    .with_columns(\n",
    "        沪深300=pl.col(\"car\") * 100_0000,\n",
    "    )\n",
    ")\n",
    "ihq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "9871a0ce-ee4b-43e3-974a-91d38ff576b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b8373a237639454786b60a0d68b00ec9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "PerspectiveWidget(binding_mode='server', columns=['ts_code', 'trade_date', 'pct_chg', 'car', '沪深300'], table_n…"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PerspectiveWidget(ihq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "id": "36e9a75c-1f00-4f25-a254-25e338ebf88d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (636, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>交易日期</th><th>资产类型</th><th>财富</th></tr><tr><td>date</td><td>str</td><td>f64</td></tr></thead><tbody><tr><td>2022-07-11</td><td>&quot;总资产&quot;</td><td>1.0035e6</td></tr><tr><td>2022-07-12</td><td>&quot;总资产&quot;</td><td>1.0082e6</td></tr><tr><td>2022-07-13</td><td>&quot;总资产&quot;</td><td>1.0143e6</td></tr><tr><td>2022-07-14</td><td>&quot;总资产&quot;</td><td>1.0105e6</td></tr><tr><td>2022-07-15</td><td>&quot;总资产&quot;</td><td>992825.88</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>2023-10-25</td><td>&quot;沪深300&quot;</td><td>791288.453778</td></tr><tr><td>2023-10-26</td><td>&quot;沪深300&quot;</td><td>793475.575065</td></tr><tr><td>2023-10-27</td><td>&quot;沪深300&quot;</td><td>804367.614284</td></tr><tr><td>2023-10-30</td><td>&quot;沪深300&quot;</td><td>809196.233072</td></tr><tr><td>2023-10-31</td><td>&quot;沪深300&quot;</td><td>806652.120115</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (636, 3)\n",
       "┌────────────┬──────────┬───────────────┐\n",
       "│ 交易日期   ┆ 资产类型 ┆ 财富          │\n",
       "│ ---        ┆ ---      ┆ ---           │\n",
       "│ date       ┆ str      ┆ f64           │\n",
       "╞════════════╪══════════╪═══════════════╡\n",
       "│ 2022-07-11 ┆ 总资产   ┆ 1.0035e6      │\n",
       "│ 2022-07-12 ┆ 总资产   ┆ 1.0082e6      │\n",
       "│ 2022-07-13 ┆ 总资产   ┆ 1.0143e6      │\n",
       "│ 2022-07-14 ┆ 总资产   ┆ 1.0105e6      │\n",
       "│ 2022-07-15 ┆ 总资产   ┆ 992825.88     │\n",
       "│ …          ┆ …        ┆ …             │\n",
       "│ 2023-10-25 ┆ 沪深300  ┆ 791288.453778 │\n",
       "│ 2023-10-26 ┆ 沪深300  ┆ 793475.575065 │\n",
       "│ 2023-10-27 ┆ 沪深300  ┆ 804367.614284 │\n",
       "│ 2023-10-30 ┆ 沪深300  ┆ 809196.233072 │\n",
       "│ 2023-10-31 ┆ 沪深300  ┆ 806652.120115 │\n",
       "└────────────┴──────────┴───────────────┘"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d5 = d4.join(\n",
    "    ihq,\n",
    "    left_on=\"交易日期\",\n",
    "    right_on=\"trade_date\",\n",
    ")\n",
    "d5 = d5.unpivot(\n",
    "    on=[\"总资产\", \"沪深300\"],\n",
    "    index=\"交易日期\",\n",
    "    variable_name=\"资产类型\",\n",
    "    value_name=\"财富\",\n",
    ")\n",
    "d5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "952f167e-fe39-4294-9a20-180199ee1266",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bfa0c60ad8734e0ea619f5538407b604",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "PerspectiveWidget(binding_mode='server', columns=['交易日期', '资产类型', '财富'], table_name='0.6572100757167281', them…"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PerspectiveWidget(d5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b668ae27-1bcb-4460-b8ea-b2450d96a8c3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
